Class: Parse::Authorization::MemoryCache
- Inherits:
-
Object
- Object
- Parse::Authorization::MemoryCache
- Defined in:
- lib/parse/authorization.rb
Overview
Default cache: a process-local hash with per-entry TTL, guarded by a
Mutex. Fine for a single process. Multi-process deployments (Puma
workers, Sidekiq processes) get one of these per process and should
install a shared plane instead, which is what
Parse::Cache::Redis#scoped(...).identity / .roles return.
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.
65 66 67 68 |
# File 'lib/parse/authorization.rb', line 65 def initialize @data = {} @mutex = Mutex.new end |
Instance Method Details
#clear ⇒ Object
Drop every entry.
101 102 103 |
# File 'lib/parse/authorization.rb', line 101 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.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/parse/authorization.rb', line 74 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
96 97 98 |
# File 'lib/parse/authorization.rb', line 96 def invalidate(key) @mutex.synchronize { @data.delete(key) } end |