Class: Smplkit::Flags::ResolutionCache
- Inherits:
-
Object
- Object
- Smplkit::Flags::ResolutionCache
- Defined in:
- lib/smplkit/flags/client.rb
Overview
Thread-safe LRU resolution cache with hit/miss stats.
Constant Summary collapse
- DEFAULT_MAX_SIZE =
10_000
Instance Attribute Summary collapse
-
#cache_hits ⇒ Object
readonly
Returns the value of attribute cache_hits.
-
#cache_misses ⇒ Object
readonly
Returns the value of attribute cache_misses.
Instance Method Summary collapse
- #clear ⇒ Object
- #get(cache_key) ⇒ Object
-
#initialize(max_size: DEFAULT_MAX_SIZE) ⇒ ResolutionCache
constructor
A new instance of ResolutionCache.
- #put(cache_key, value) ⇒ Object
Constructor Details
#initialize(max_size: DEFAULT_MAX_SIZE) ⇒ ResolutionCache
Returns a new instance of ResolutionCache.
36 37 38 39 40 41 42 |
# File 'lib/smplkit/flags/client.rb', line 36 def initialize(max_size: DEFAULT_MAX_SIZE) @max_size = max_size @cache = {} @lock = Mutex.new @cache_hits = 0 @cache_misses = 0 end |
Instance Attribute Details
#cache_hits ⇒ Object (readonly)
Returns the value of attribute cache_hits.
34 35 36 |
# File 'lib/smplkit/flags/client.rb', line 34 def cache_hits @cache_hits end |
#cache_misses ⇒ Object (readonly)
Returns the value of attribute cache_misses.
34 35 36 |
# File 'lib/smplkit/flags/client.rb', line 34 def cache_misses @cache_misses end |
Instance Method Details
#clear ⇒ Object
66 67 68 |
# File 'lib/smplkit/flags/client.rb', line 66 def clear @lock.synchronize { @cache.clear } end |
#get(cache_key) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/smplkit/flags/client.rb', line 44 def get(cache_key) @lock.synchronize do if @cache.key?(cache_key) value = @cache.delete(cache_key) @cache[cache_key] = value @cache_hits += 1 [true, value] else @cache_misses += 1 [false, nil] end end end |
#put(cache_key, value) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/smplkit/flags/client.rb', line 58 def put(cache_key, value) @lock.synchronize do @cache.delete(cache_key) if @cache.key?(cache_key) @cache[cache_key] = value @cache.shift while @cache.size > @max_size end end |