class CFC::Cache
Public Class Methods
new()
click to toggle source
# File lib/cfc/cache.rb, line 6 def initialize @cache = {} end
Public Instance Methods
[](key)
click to toggle source
# File lib/cfc/cache.rb, line 14 def [](key) valid?(key) ? @cache[key].data : nil end
[]=(key, value)
click to toggle source
# File lib/cfc/cache.rb, line 18 def []=(key, value) @cache[key] = OpenStruct.new(data: value, expiry: nil) end
fetch(key, expiry: nil) { |: nil| ... }
click to toggle source
# File lib/cfc/cache.rb, line 30 def fetch(key, expiry: nil) if valid?(key) @cache[key].data else data = block_given? ? yield : nil write(key, data, expiry: expiry) data end end
include?(key)
click to toggle source
# File lib/cfc/cache.rb, line 10 def include?(key) @cache.include?(key) && (@cache[key].expiry.nil? || @cache[key].expiry >= DateTime.now.to_time.to_i) end
Also aliased as: valid?
read(key)
click to toggle source
# File lib/cfc/cache.rb, line 26 def read(key) valid?(key) ? @cache[key].data : nil end
write(key, value, expiry: nil)
click to toggle source
# File lib/cfc/cache.rb, line 22 def write(key, value, expiry: nil) @cache[key] = OpenStruct.new(data: value, expiry: expiry) end