Class: RVC::TTLCache

Inherits:
Object
  • Object
show all
Defined in:
lib/rvc/ttl_cache.rb

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initialize(ttl) ⇒ TTLCache

Returns a new instance of TTLCache.



26
27
28
29
# File 'lib/rvc/ttl_cache.rb', line 26

def initialize ttl
  @ttl = ttl
  @cache = {}
end

Instance Method Details

#[](obj, sym, *args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rvc/ttl_cache.rb', line 31

def [] obj, sym, *args
  @cache.delete_if { |k,v| v.time + @ttl < Time.now }
  key = [obj,sym,*args]
  if e = @cache[key]
    e.value
  else
    value = obj.send(sym, *args)
    @cache[key] = Entry.new value, Time.now
    value
  end
end