Class: Lutaml::Hal::Cache::SimpleCacheStore
- Inherits:
-
Object
- Object
- Lutaml::Hal::Cache::SimpleCacheStore
- Defined in:
- lib/lutaml/hal/cache/simple_cache_store.rb
Overview
Simple in-memory cache store for testing and fallback scenarios.
Thread-safe: a single mutex guards the cache and its LRU bookkeeping so it can back the register cache when consumers realize links from many threads (e.g. a parallel fetcher).
Instance Attribute Summary collapse
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
Instance Method Summary collapse
- #cache_info ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #get(key) ⇒ Object
-
#initialize(max_size = 100) ⇒ SimpleCacheStore
constructor
A new instance of SimpleCacheStore.
- #set(key, value) ⇒ Object
- #size ⇒ Object
- #stats ⇒ Object
Constructor Details
#initialize(max_size = 100) ⇒ SimpleCacheStore
Returns a new instance of SimpleCacheStore.
14 15 16 17 18 19 |
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 14 def initialize(max_size = 100) @max_size = max_size @cache = {} @access_order = [] @mutex = Mutex.new end |
Instance Attribute Details
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
12 13 14 |
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 12 def max_size @max_size end |
Instance Method Details
#cache_info ⇒ Object
77 78 79 |
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 77 def cache_info stats end |
#clear ⇒ Object
56 57 58 59 60 61 |
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 56 def clear @mutex.synchronize do @cache.clear @access_order.clear end end |
#delete(key) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 49 def delete(key) @mutex.synchronize do @access_order.delete(key) @cache.delete(key) end end |
#get(key) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 21 def get(key) @mutex.synchronize do return nil unless @cache.key?(key) # Update access order for LRU @access_order.delete(key) @access_order.push(key) @cache[key] end end |
#set(key, value) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 33 def set(key, value) @mutex.synchronize do # Remove existing entry if present if @cache.key?(key) @access_order.delete(key) elsif @cache.size >= @max_size # Evict least recently used item lru_key = @access_order.shift @cache.delete(lru_key) end @cache[key] = value @access_order.push(key) end end |
#size ⇒ Object
63 64 65 |
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 63 def size @mutex.synchronize { @cache.size } end |
#stats ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/lutaml/hal/cache/simple_cache_store.rb', line 67 def stats @mutex.synchronize do { size: @cache.size, max_size: @max_size, keys: @cache.keys } end end |