Module: Jekyll::Minifier::CompressorCache

Defined in:
lib/jekyll-minifier.rb

Overview

CompressorCache module provides thread-safe caching for compressor objects to improve performance by reusing configured compressor instances

Constant Summary collapse

MAX_CACHE_SIZE =

Maximum cache size per compressor type (reasonable memory limit)

10

Class Method Summary collapse

Class Method Details

.cache_sizesHash

Get cache sizes (for monitoring)

Returns:

  • (Hash)

    Current cache sizes by type



304
305
306
307
308
309
310
311
312
313
# File 'lib/jekyll-minifier.rb', line 304

def cache_sizes
  @cache_mutex.synchronize do
    {
      css: @compressor_caches[:css].size,
      js: @compressor_caches[:js].size,
      html: @compressor_caches[:html].size,
      total: @compressor_caches.values.sum(&:size)
    }
  end
end

.clear_allObject

Clear all caches (useful for testing and memory management)



289
290
291
292
293
294
# File 'lib/jekyll-minifier.rb', line 289

def clear_all
  @cache_mutex.synchronize do
    @compressor_caches.each { |_, cache| cache.clear }
    @cache_stats = { hits: 0, misses: 0, evictions: 0 }
  end
end

.generate_cache_key(config_hash) ⇒ String

Generate cache key from configuration hash

Parameters:

  • config_hash (Hash)

    Configuration parameters

Returns:

  • (String)

    Unique cache key



278
279
280
281
282
283
284
285
286
# File 'lib/jekyll-minifier.rb', line 278

def generate_cache_key(config_hash)
  return 'default' if config_hash.nil? || config_hash.empty?

  # Sort keys for consistent hashing
  sorted_config = config_hash.sort.to_h
  # Use SHA256 for consistent, collision-resistant keys
  require 'digest'
  Digest::SHA256.hexdigest(sorted_config.to_s)[0..16] # First 16 chars for brevity
end

.get_or_create(type, cache_key, &factory_block) ⇒ Object

Get cached compressor or create and cache new one

Parameters:

  • type (Symbol)

    Compressor type (:css, :js, :html)

  • cache_key (String)

    Unique key for this configuration

  • factory_block (Proc)

    Block that creates the compressor if not cached

Returns:

  • (Object)

    Cached or newly created compressor instance



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/jekyll-minifier.rb', line 247

def get_or_create(type, cache_key, &factory_block)
  @cache_mutex.synchronize do
    cache = @compressor_caches[type]

    if cache.key?(cache_key)
      # Cache hit - move to end for LRU
      compressor = cache.delete(cache_key)
      cache[cache_key] = compressor
      @cache_stats[:hits] += 1
      compressor
    else
      # Cache miss - create new compressor
      compressor = factory_block.call

      # Evict oldest entry if cache is full
      if cache.size >= MAX_CACHE_SIZE
        evicted_key = cache.keys.first
        cache.delete(evicted_key)
        @cache_stats[:evictions] += 1
      end

      cache[cache_key] = compressor
      @cache_stats[:misses] += 1
      compressor
    end
  end
end

.hit_ratioFloat

Check if caching is effectively working

Returns:

  • (Float)

    Cache hit ratio (0.0 to 1.0)



317
318
319
320
321
322
323
# File 'lib/jekyll-minifier.rb', line 317

def hit_ratio
  @cache_mutex.synchronize do
    total = @cache_stats[:hits] + @cache_stats[:misses]
    return 0.0 if total == 0
    @cache_stats[:hits].to_f / total
  end
end

.statsHash

Get cache statistics (for monitoring and testing)

Returns:

  • (Hash)

    Cache hit/miss/eviction statistics



298
299
300
# File 'lib/jekyll-minifier.rb', line 298

def stats
  @cache_mutex.synchronize { @cache_stats.dup }
end