Class: APICache::MemoryStore
- Inherits:
-
AbstractStore
- Object
- AbstractStore
- APICache::MemoryStore
- Defined in:
- lib/api_cache/memory_store.rb
Instance Method Summary collapse
- #exists?(key) ⇒ Boolean
- #expired?(key, timeout) ⇒ Boolean
- #get(key) ⇒ Object
-
#initialize ⇒ MemoryStore
constructor
A new instance of MemoryStore.
- #set(key, value) ⇒ Object
Constructor Details
#initialize ⇒ MemoryStore
Returns a new instance of MemoryStore.
3 4 5 6 7 |
# File 'lib/api_cache/memory_store.rb', line 3 def initialize APICache.logger.debug "Using memory store" @cache = {} true end |
Instance Method Details
#exists?(key) ⇒ Boolean
21 22 23 |
# File 'lib/api_cache/memory_store.rb', line 21 def exists?(key) !@cache[key].nil? end |
#expired?(key, timeout) ⇒ Boolean
25 26 27 |
# File 'lib/api_cache/memory_store.rb', line 25 def expired?(key, timeout) Time.now - created(key) > timeout end |
#get(key) ⇒ Object
15 16 17 18 19 |
# File 'lib/api_cache/memory_store.rb', line 15 def get(key) data = @cache[key][1] APICache.logger.debug("cache: #{data.nil? ? "miss" : "hit"} (#{key})") data end |
#set(key, value) ⇒ Object
9 10 11 12 13 |
# File 'lib/api_cache/memory_store.rb', line 9 def set(key, value) APICache.logger.debug("cache: set (#{key})") @cache[key] = [Time.now, value] true end |