Class: Airtable::ORM::Configuration::MemoryCache
- Inherits:
-
Object
- Object
- Airtable::ORM::Configuration::MemoryCache
- Defined in:
- lib/airtable/orm/configuration.rb
Overview
Default in-process schema cache so an unconfigured consumer doesn't hit the /v0/meta API on every Schema.fetch (and burn the 5 RPS budget on metadata). Hosts inject a real store (Rails.cache); this one honours only :expires_in. The mutex is held across the fetch block (all keys serialize behind it, and nesting fetch calls would deadlock) — fine for the rare schema refresh this exists for.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #fetch(key, expires_in: nil) ⇒ Object
-
#initialize ⇒ MemoryCache
constructor
A new instance of MemoryCache.
Constructor Details
#initialize ⇒ MemoryCache
Returns a new instance of MemoryCache.
46 47 48 49 |
# File 'lib/airtable/orm/configuration.rb', line 46 def initialize @mutex = Mutex.new @store = {} end |
Instance Method Details
#delete(key) ⇒ Object
64 65 66 |
# File 'lib/airtable/orm/configuration.rb', line 64 def delete(key) @mutex.synchronize { @store.delete(key) } end |
#fetch(key, expires_in: nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/airtable/orm/configuration.rb', line 51 def fetch(key, expires_in: nil) @mutex.synchronize do # Hit/miss by key presence, not value truthiness — false/nil are cacheable # (matching Rails.cache semantics). if (entry = @store[key]) value, expires_at = entry return value if expires_at.nil? || Time.now < expires_at end yield.tap { |fresh| @store[key] = [fresh, expires_in && (Time.now + expires_in)] } end end |