Class: Parse::AtlasSearch::Session::MemoryCache
- Inherits:
-
Object
- Object
- Parse::AtlasSearch::Session::MemoryCache
- Defined in:
- lib/parse/atlas_search/session.rb
Overview
Default cache: in-memory hash with per-entry TTL, guarded by a Mutex. Suitable for single-process apps. Apps running multi-process (Puma workers, Sidekiq processes) get a per- process cache — install a shared cache through Parse::AtlasSearch.session_cache= for cross-process sharing.
Instance Method Summary collapse
-
#clear ⇒ Object
Drop every entry.
-
#get(key) ⇒ Object?
The cached value, or
nilwhen the key is missing or its TTL has elapsed. -
#initialize ⇒ MemoryCache
constructor
A new instance of MemoryCache.
- #invalidate(key) ⇒ Object
- #set(key, value, ttl:) ⇒ Object
Constructor Details
#initialize ⇒ MemoryCache
Returns a new instance of MemoryCache.
58 59 60 61 |
# File 'lib/parse/atlas_search/session.rb', line 58 def initialize @data = {} @mutex = Mutex.new end |
Instance Method Details
#clear ⇒ Object
Drop every entry. Used by Parse::AtlasSearch::Session.reset_caches! and by tests that need a clean slate.
95 96 97 |
# File 'lib/parse/atlas_search/session.rb', line 95 def clear @mutex.synchronize { @data.clear } end |
#get(key) ⇒ Object?
Returns the cached value, or nil when the key is missing or its TTL has elapsed. Expired entries are evicted lazily on read.
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/parse/atlas_search/session.rb', line 67 def get(key) @mutex.synchronize do entry = @data[key] return nil if entry.nil? if entry[:expires_at] < Time.now @data.delete(key) return nil end entry[:value] end end |
#invalidate(key) ⇒ Object
89 90 91 |
# File 'lib/parse/atlas_search/session.rb', line 89 def invalidate(key) @mutex.synchronize { @data.delete(key) } end |