Class: Datacenter::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/datacenter/cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(expiration_time = nil) ⇒ Cache

Returns a new instance of Cache.



4
5
6
7
# File 'lib/datacenter/cache.rb', line 4

def initialize(expiration_time=nil)
  @expiration_time = expiration_time
  @data = {}
end

Instance Method Details

#fetch(key, &block) ⇒ Object



9
10
11
12
# File 'lib/datacenter/cache.rb', line 9

def fetch(key, &block)
  set key, block.call if !data.key?(key) || expired?(key)
  get key
end