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
-
.cache_sizes ⇒ Hash
Get cache sizes (for monitoring).
-
.clear_all ⇒ Object
Clear all caches (useful for testing and memory management).
-
.generate_cache_key(config_hash) ⇒ String
Generate cache key from configuration hash.
-
.get_or_create(type, cache_key, &factory_block) ⇒ Object
Get cached compressor or create and cache new one.
-
.hit_ratio ⇒ Float
Check if caching is effectively working.
-
.stats ⇒ Hash
Get cache statistics (for monitoring and testing).
Class Method Details
.cache_sizes ⇒ Hash
Get cache sizes (for monitoring)
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_all ⇒ Object
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
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
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_ratio ⇒ Float
Check if caching is effectively working
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 |
.stats ⇒ Hash
Get cache statistics (for monitoring and testing)
380 381 382 |
# File 'lib/jekyll-minifier.rb', line 380 def stats @cache_mutex.synchronize { @cache_stats.dup } end |