class Datacenter::Cache
Attributes
data[R]
expiration_time[R]
Public Class Methods
new(expiration_time=nil)
click to toggle source
# File lib/datacenter/cache.rb, line 4 def initialize(expiration_time=nil) @expiration_time = expiration_time @data = {} end
Public Instance Methods
fetch(key, &block)
click to toggle source
# File lib/datacenter/cache.rb, line 9 def fetch(key, &block) set key, block.call if !data.key?(key) || expired?(key) get key end
Private Instance Methods
expired?(key)
click to toggle source
# File lib/datacenter/cache.rb, line 29 def expired?(key) return false unless expiration_time Time.now >= data[key][:fetched_at] + expiration_time end
get(key)
click to toggle source
# File lib/datacenter/cache.rb, line 18 def get(key) data[key][:value] end
set(key, value)
click to toggle source
# File lib/datacenter/cache.rb, line 22 def set(key, value) data[key] = { value: value, fetched_at: Time.now } end