Class: Wonderfl::Utils::Cache
- Inherits:
-
PStore
- Object
- PStore
- Wonderfl::Utils::Cache
- Defined in:
- lib/wonderfl/utils.rb
Overview
Caches response data from APIs
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(filename, expire) ⇒ Cache
constructor
A new instance of Cache.
Constructor Details
#initialize(filename, expire) ⇒ Cache
Returns a new instance of Cache.
16 17 18 19 20 |
# File 'lib/wonderfl/utils.rb', line 16 def initialize(filename, expire) @filename = filename || 'wonderfl_cache.db' @expire = expire || 0 super(@filename) end |
Instance Method Details
#[](key) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/wonderfl/utils.rb', line 22 def [](key) value = super(key) || {} expired = (value[:timestamp].nil? || value[:timestamp] + @expire < Time.now) expired ? nil : value[:entity] end |
#[]=(key, value) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/wonderfl/utils.rb', line 29 def []=(key, value) super(key, { :entity => value, :timestamp => Time.now }) end |