Class: Airtable::ORM::Configuration::MemoryCache

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeMemoryCache

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