Class: Arturo::FeatureCaching::Cache
- Inherits:
-
Object
- Object
- Arturo::FeatureCaching::Cache
- Defined in:
- lib/arturo/feature_caching.rb
Overview
Quack like a Rails cache.
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #read(name, options = nil) ⇒ Object
- #write(name, value, options = nil) ⇒ Object
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
53 54 55 |
# File 'lib/arturo/feature_caching.rb', line 53 def initialize @data = {} # of the form {key => [value, expires_at or nil]} end |
Instance Method Details
#clear ⇒ Object
76 77 78 |
# File 'lib/arturo/feature_caching.rb', line 76 def clear @data.clear end |
#read(name, options = nil) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/arturo/feature_caching.rb', line 56 def read(name, = nil) name = name.to_s value, expires_at = *@data[name] if value && (expires_at.blank? || expires_at > Time.now) value else nil end end |
#write(name, value, options = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/arturo/feature_caching.rb', line 65 def write(name, value, = nil) name = name.to_s expires_at = if && .respond_to?(:[]) && [:expires_in] Time.now + .delete(:expires_in) else nil end value.freeze.tap do |val| @data[name] = [value, expires_at] end end |