Class: APICache::MemoryStore

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

Instance Method Summary (collapse)

Constructor Details

- (MemoryStore) initialize



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

- (Object) delete(key)



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

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

- (Boolean) exists?(key)



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

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

- (Boolean) expired?(key, timeout)



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

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

- (Object) get(key)



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

- (Object) set(key, value)



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