Class: Igniter::Store::ReadCache
- Inherits:
-
Object
- Object
- Igniter::Store::ReadCache
- Includes:
- MonitorMixin
- Defined in:
- lib/igniter/store/read_cache.rb
Constant Summary collapse
- DEFAULT_LRU_CAP =
1_000
Instance Method Summary collapse
- #get(store:, key:, as_of: nil, ttl: nil) ⇒ Object
- #get_scope(store:, scope:, as_of: nil, ttl: nil) ⇒ Object
-
#initialize(lru_cap: DEFAULT_LRU_CAP) ⇒ ReadCache
constructor
A new instance of ReadCache.
-
#invalidate(store:, key: nil, scope_changes: {}) ⇒ Object
scope_changesis a Hash of { scope_name => :changed | :unchanged | :unknown } produced by IgniterStore#update_scope_indices. - #lru_size ⇒ Object
- #put(store:, key:, fact:, as_of: nil) ⇒ Object
- #put_scope(store:, scope:, facts:, as_of: nil) ⇒ Object
- #register_consumer(store, callable) ⇒ Object
- #register_scope_consumer(store, scope, callable) ⇒ Object
Constructor Details
#initialize(lru_cap: DEFAULT_LRU_CAP) ⇒ ReadCache
Returns a new instance of ReadCache.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/igniter/store/read_cache.rb', line 12 def initialize(lru_cap: DEFAULT_LRU_CAP) super() @entries = {} @consumers = Hash.new { |hash, key| hash[key] = [] } @scope_consumers = Hash.new { |hash, key| hash[key] = [] } @lru_cap = lru_cap # Tracks insertion/access order for time-travel cache entries only. # Current-state entries (as_of: nil) live until explicit invalidation. # Ruby Hash preserves insertion order; delete+reinsert = move to MRU. @lru_order = {} end |
Instance Method Details
#get(store:, key:, as_of: nil, ttl: nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/igniter/store/read_cache.rb', line 32 def get(store:, key:, as_of: nil, ttl: nil) cache_key = [store, key, as_of] entry = synchronize do e = @entries[cache_key] if e && as_of @lru_order.delete(cache_key) @lru_order[cache_key] = true end e end return nil unless entry if ttl age = Process.clock_gettime(Process::CLOCK_REALTIME) - entry.fetch(:cached_at) return nil if age > ttl end entry.fetch(:fact) end |
#get_scope(store:, scope:, as_of: nil, ttl: nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/igniter/store/read_cache.rb', line 66 def get_scope(store:, scope:, as_of: nil, ttl: nil) cache_key = [:scope, store, scope, as_of] entry = synchronize do e = @entries[cache_key] if e && as_of @lru_order.delete(cache_key) @lru_order[cache_key] = true end e end return nil unless entry if ttl age = Process.clock_gettime(Process::CLOCK_REALTIME) - entry.fetch(:cached_at) return nil if age > ttl end entry.fetch(:facts) end |
#invalidate(store:, key: nil, scope_changes: {}) ⇒ Object
scope_changes is a Hash of { scope_name => :changed | :unchanged | :unknown } produced by IgniterStore#update_scope_indices. Scope consumers are only notified for scopes that are :changed or :unknown (conservative). Scopes marked :unchanged are skipped — their membership did not change and firing their consumers would be a false-positive thundering herd.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/igniter/store/read_cache.rb', line 105 def invalidate(store:, key: nil, scope_changes: {}) point_targets, scope_notifications = synchronize do affected_scopes = [] @entries.delete_if do |cache_key, _entry| should_delete = if cache_key[0] == :scope && cache_key[1] == store affected_scopes << cache_key[2] true else cache_key[0] == store && (key.nil? || cache_key[1] == key) end @lru_order.delete(cache_key) if should_delete should_delete end notify_scopes = affected_scopes.uniq.reject do |scope| scope_changes[scope] == :unchanged end scope_notifs = notify_scopes.map do |scope| [scope, @scope_consumers[[store, scope]].dup] end [@consumers[store].dup, scope_notifs] end point_targets.each { |t| notify(t, store, key) } scope_notifications.each do |scope, targets| targets.each { |t| notify_scope(t, store, scope) } end end |
#lru_size ⇒ Object
136 137 138 |
# File 'lib/igniter/store/read_cache.rb', line 136 def lru_size synchronize { @lru_order.size } end |
#put(store:, key:, fact:, as_of: nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/igniter/store/read_cache.rb', line 52 def put(store:, key:, fact:, as_of: nil) cache_key = [store, key, as_of] synchronize do @entries[cache_key] = { fact: fact, cached_at: Process.clock_gettime(Process::CLOCK_REALTIME) } if as_of @lru_order[cache_key] = true evict_lru_if_needed end end end |
#put_scope(store:, scope:, facts:, as_of: nil) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/igniter/store/read_cache.rb', line 86 def put_scope(store:, scope:, facts:, as_of: nil) cache_key = [:scope, store, scope, as_of] synchronize do @entries[cache_key] = { facts: facts, cached_at: Process.clock_gettime(Process::CLOCK_REALTIME) } if as_of @lru_order[cache_key] = true evict_lru_if_needed end end end |
#register_consumer(store, callable) ⇒ Object
24 25 26 |
# File 'lib/igniter/store/read_cache.rb', line 24 def register_consumer(store, callable) synchronize { @consumers[store] << callable } end |
#register_scope_consumer(store, scope, callable) ⇒ Object
28 29 30 |
# File 'lib/igniter/store/read_cache.rb', line 28 def register_scope_consumer(store, scope, callable) synchronize { @scope_consumers[[store, scope]] << callable } end |