Class: APICache::MemcacheStore

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

Defined Under Namespace

Classes: NotDefined, NotReady

Instance Method Summary collapse

Constructor Details

#initializeMemcacheStore

Returns a new instance of MemcacheStore.

Raises:



16
17
18
19
20
21
22
23
24
25
# File 'lib/api_cache/memcache_store.rb', line 16

def initialize
  APICache.logger.log "Using memcached store"
  namespace = 'api_cache'
  host = '127.0.0.1:11211'
  @memcache = MemCache.new(host, {:namespace => namespace})
  raise NotReady unless @memcache.active?
  true
# rescue NameError
#   raise NotDefined
end

Instance Method Details

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/api_cache/memcache_store.rb', line 40

def exists?(key)
  # TODO: inefficient - is there a better way?
  !@memcache.get(key).nil?
end

#expired?(key, timeout) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/api_cache/memcache_store.rb', line 45

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

#get(key) ⇒ Object



34
35
36
37
38
# File 'lib/api_cache/memcache_store.rb', line 34

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

#set(key, data) ⇒ Object



27
28
29
30
31
32
# File 'lib/api_cache/memcache_store.rb', line 27

def set(key, data)
  @memcache.set(key, data)
  @memcache.set("#{key}_created_at", Time.now)
  APICache.logger.log("cache: set (#{key})")
  true
end