Class: Togul::Cache
- Inherits:
-
Object
- Object
- Togul::Cache
- Defined in:
- lib/togul/cache.rb
Instance Method Summary collapse
- #flush ⇒ Object
-
#get(key) ⇒ EvaluateResult?
Cached result or nil on miss/expiry/stale.
-
#initialize(ttl:) ⇒ Cache
constructor
A new instance of Cache.
- #invalidate_flag(flag_key) ⇒ Object
- #set(key, result) ⇒ Object
Constructor Details
#initialize(ttl:) ⇒ Cache
Returns a new instance of Cache.
5 6 7 8 9 |
# File 'lib/togul/cache.rb', line 5 def initialize(ttl:) @ttl = ttl @store = {} @mutex = Mutex.new end |
Instance Method Details
#flush ⇒ Object
35 36 37 |
# File 'lib/togul/cache.rb', line 35 def flush @mutex.synchronize { @store.clear } end |
#get(key) ⇒ EvaluateResult?
Returns cached result or nil on miss/expiry/stale.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/togul/cache.rb', line 12 def get(key) @mutex.synchronize do entry = @store[key] return nil unless entry return nil if Time.now.to_f > entry[:expires_at] result = entry[:value] # Treat entries with blank value_type as stale (legacy/invalid format). return nil if result.value_type.to_s.empty? result end end |
#invalidate_flag(flag_key) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/togul/cache.rb', line 39 def invalidate_flag(flag_key) prefix = "#{flag_key}:" @mutex.synchronize do @store.delete_if { |key, _| key.start_with?(prefix) } end end |
#set(key, result) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/togul/cache.rb', line 26 def set(key, result) @mutex.synchronize do @store[key] = { value: result, expires_at: Time.now.to_f + @ttl } end end |