Class: APICache::MemoryStore

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

Instance Method Summary collapse

Constructor Details

#initializeMemoryStore

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

#delete(key) ⇒ Object



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

def delete(key)
  @cache.delete(key)
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#expired?(key, timeout) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/api_cache/memory_store.rb', line 29

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