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



386
387
388
389
390
391
392
393
394
395
# File 'lib/jekyll-minifier.rb', line 386

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)



371
372
373
374
375
376
# File 'lib/jekyll-minifier.rb', line 371

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



360
361
362
363
364
365
366
367
368
# File 'lib/jekyll-minifier.rb', line 360

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



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/jekyll-minifier.rb', line 329

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)



399
400
401
402
403
404
405
# File 'lib/jekyll-minifier.rb', line 399

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



380
381
382
# File 'lib/jekyll-minifier.rb', line 380

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