Class: APICache::MemoryStore

Inherits:
AbstractStore show all
Defined in:
lib/api_cache/memory_store.rb

Instance Method Summary collapse

Constructor Details

#initializeMemoryStore



2
3
4
5
6
# File 'lib/api_cache/memory_store.rb', line 2

def initialize
  APICache.logger.log "Using memory store"
  @cache = {}
  true
end

Instance Method Details

#exists?(key) ⇒ Boolean



20
21
22
# File 'lib/api_cache/memory_store.rb', line 20

def exists?(key)
  !@cache[key].nil?
end

#expired?(key, timeout) ⇒ Boolean



24
25
26
# File 'lib/api_cache/memory_store.rb', line 24

def expired?(key, timeout)
  Time.now - created(key) > timeout
end

#get(key) ⇒ Object



14
15
16
17
18
# File 'lib/api_cache/memory_store.rb', line 14

def get(key)
  data = @cache[key][1]
  APICache.logger.log("cache: #{data.nil? ? "miss" : "hit"} (#{key})")
  data
end

#set(key, value) ⇒ Object



8
9
10
11
12
# File 'lib/api_cache/memory_store.rb', line 8

def set(key, value)
  APICache.logger.log("cache: set (#{key})")
  @cache[key] = [Time.now, value]
  true
end