Class: SafeMemoize::Stores::Multilevel
- Defined in:
- lib/safe_memoize/stores/multilevel.rb
Overview
Multi-level (L1/L2/…) cache store that checks faster layers first and promotes values up on a miss, reducing latency and load on slower backends.
Reads walk the store list from first (fastest) to last (slowest). On a miss at level N the value is read from level N+1 and written back into all preceding levels ("read-through promotion"). Writes always go to every level so all layers stay consistent.
Constant Summary
Constants inherited from Base
Instance Attribute Summary collapse
-
#stores ⇒ Array<Stores::Base>
readonly
The ordered store layers (fastest first).
Instance Method Summary collapse
-
#clear ⇒ Object
Clear every level.
-
#delete(key) ⇒ Object
Delete from every level.
-
#initialize(*stores, promote_expires_in: nil) ⇒ Multilevel
constructor
A new instance of Multilevel.
-
#keys ⇒ Object
Union of live keys across all levels.
-
#read(key) ⇒ Object
Walk levels from fastest to slowest; return the first hit, promoting the value into all shallower layers.
-
#write(key, value, expires_in: nil) ⇒ Object
Write to every level simultaneously.
Methods inherited from Base
Constructor Details
#initialize(*stores, promote_expires_in: nil) ⇒ Multilevel
Returns a new instance of Multilevel.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/safe_memoize/stores/multilevel.rb', line 36 def initialize(*stores, promote_expires_in: nil) raise ArgumentError, "Multilevel requires at least 2 stores" if stores.size < 2 stores.each_with_index do |s, i| unless s.is_a?(Base) raise ArgumentError, "Multilevel store[#{i}] must be a Stores::Base instance (got #{s.class})" end end @stores = stores.freeze @promote_expires_in = promote_expires_in ? Float(promote_expires_in) : nil end |
Instance Attribute Details
#stores ⇒ Array<Stores::Base> (readonly)
Returns the ordered store layers (fastest first).
26 27 28 |
# File 'lib/safe_memoize/stores/multilevel.rb', line 26 def stores @stores end |
Instance Method Details
#clear ⇒ Object
Clear every level.
76 77 78 |
# File 'lib/safe_memoize/stores/multilevel.rb', line 76 def clear @stores.each(&:clear) end |
#delete(key) ⇒ Object
Delete from every level.
71 72 73 |
# File 'lib/safe_memoize/stores/multilevel.rb', line 71 def delete(key) @stores.each { |s| s.delete(key) } end |
#keys ⇒ Object
Union of live keys across all levels.
81 82 83 |
# File 'lib/safe_memoize/stores/multilevel.rb', line 81 def keys @stores.flat_map(&:keys).uniq end |
#read(key) ⇒ Object
Walk levels from fastest to slowest; return the first hit, promoting the value into all shallower layers.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/safe_memoize/stores/multilevel.rb', line 52 def read(key) @stores.each_with_index do |store, i| result = store.read(key) next if result.equal?(MISS) # Promote into every shallower level @stores.first(i).each { |s| s.write(key, result, expires_in: @promote_expires_in) } return result end MISS end |
#write(key, value, expires_in: nil) ⇒ Object
Write to every level simultaneously.
66 67 68 |
# File 'lib/safe_memoize/stores/multilevel.rb', line 66 def write(key, value, expires_in: nil) @stores.each { |s| s.write(key, value, expires_in: expires_in) } end |