Module: JLPT::CacheManager
- Defined in:
- lib/jlpt/core/cache_manager.rb
Overview
Thread-safe LRU Cache Manager for JLPT Text Analysis.
Caches analysis results to prevent redundant processing for identical texts.
Constant Summary collapse
- MUTEX =
Mutex.new
- DEFAULT_CAPACITY =
1000
Class Method Summary collapse
-
.capacity ⇒ Integer
Maximum capacity.
-
.capacity=(cap) ⇒ Object
Set capacity.
-
.clear ⇒ Object
Clear the cache store.
-
.disable! ⇒ Object
Disable caching.
-
.enable! ⇒ Object
Enable caching.
-
.enabled? ⇒ Boolean
Check if cache is enabled.
-
.fetch(key) { ... } ⇒ Object
Fetch cached result or compute via block.
-
.size ⇒ Integer
Current cache size.
Class Method Details
.capacity ⇒ Integer
Maximum capacity
58 59 60 |
# File 'lib/jlpt/core/cache_manager.rb', line 58 def capacity @capacity ||= DEFAULT_CAPACITY end |
.capacity=(cap) ⇒ Object
Set capacity
63 64 65 66 67 68 |
# File 'lib/jlpt/core/cache_manager.rb', line 63 def capacity=(cap) MUTEX.synchronize do @capacity = cap store.shift while store.length > cap end end |
.clear ⇒ Object
Clear the cache store
26 27 28 |
# File 'lib/jlpt/core/cache_manager.rb', line 26 def clear MUTEX.synchronize { store.clear } end |
.disable! ⇒ Object
Disable caching
51 52 53 |
# File 'lib/jlpt/core/cache_manager.rb', line 51 def disable! @enabled = false end |
.enable! ⇒ Object
Enable caching
46 47 48 |
# File 'lib/jlpt/core/cache_manager.rb', line 46 def enable! @enabled = true end |
.enabled? ⇒ Boolean
Check if cache is enabled
40 41 42 43 |
# File 'lib/jlpt/core/cache_manager.rb', line 40 def enabled? @enabled = true if @enabled.nil? @enabled end |
.fetch(key) { ... } ⇒ Object
Fetch cached result or compute via block
19 20 21 22 23 |
# File 'lib/jlpt/core/cache_manager.rb', line 19 def fetch(key, &block) return yield unless enabled? && key.is_a?(String) && !key.empty? MUTEX.synchronize { read_or_write_cache(key, &block) } end |
.size ⇒ Integer
Current cache size
33 34 35 |
# File 'lib/jlpt/core/cache_manager.rb', line 33 def size MUTEX.synchronize { store.length } end |