class BluntCache
In-memory cache service.
Constants
- VERSION
Public Class Methods
data()
click to toggle source
# File lib/blunt-cache.rb, line 44 def self.data @data||= {} end
expire_default()
click to toggle source
# File lib/blunt-cache.rb, line 52 def self.expire_default @expire_default||=60 end
fetch(key, options = {}, &block)
click to toggle source
Get key
from cache. Executes block
, stores it's result and returns it if not set or expired.
def self.fetch(key, expire: nil, &block)
# File lib/blunt-cache.rb, line 27 def self.fetch(key, options = {}, &block) expire = options[:expire] if self.key?(key) self.data[key] else result = block.call self.set key, result, :expire => expire result end end
flush()
click to toggle source
Clear cache
# File lib/blunt-cache.rb, line 39 def self.flush @data = {} @timestamp = {} end
get(key)
click to toggle source
Get key
from cache. Returns nil if not set or expired.
# File lib/blunt-cache.rb, line 15 def self.get(key) self.timestamp[key].is_a?(Time) && Time.now < self.timestamp[key] ? self.data[key] : nil end
key?(key)
click to toggle source
Checks if key present in store. Returns true if key exists (even if value is false or nil) and false if key doesn't exist or expired.
# File lib/blunt-cache.rb, line 21 def self.key?(key) self.data.key?(key) && self.timestamp[key].is_a?(Time) && Time.now < self.timestamp[key] end
set(key, data, options = {})
click to toggle source
Store data
in cache by key
for :expire
seconds (default is 60 sec)
def self.set(key, data, expire: nil)
# File lib/blunt-cache.rb, line 7 def self.set(key, data, options = {}) expire = options[:expire] self.timestamp[key] = Time.now + (expire || self.expire_default) self.data[key] = data data end
timestamp()
click to toggle source
# File lib/blunt-cache.rb, line 48 def self.timestamp @timestamp||= {} end