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
- #delete(name) ⇒ 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.
103 104 105 |
# File 'lib/arturo/feature_caching.rb', line 103 def initialize @data = {} # of the form {key => [value, expires_at or nil]} end |
Instance Method Details
#clear ⇒ Object
131 132 133 |
# File 'lib/arturo/feature_caching.rb', line 131 def clear @data.clear end |
#delete(name) ⇒ Object
116 117 118 |
# File 'lib/arturo/feature_caching.rb', line 116 def delete(name) @data.delete(name) end |
#read(name, options = nil) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/arturo/feature_caching.rb', line 107 def read(name, = nil) 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
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/arturo/feature_caching.rb', line 120 def write(name, value, = nil) 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 |