Class: EAAL::Cache::MemcachedCache

Inherits:
Object
  • Object
show all
Defined in:
lib/eaal/cache/memcached.rb

Instance Method Summary collapse

Constructor Details

#initialize(servers = "localhost:11211", options = {}) ⇒ MemcachedCache

Returns a new instance of MemcachedCache.



6
7
8
9
# File 'lib/eaal/cache/memcached.rb', line 6

def initialize(servers="localhost:11211",options={})
  o = {:namespace => 'eaal'}.merge(options)
  $cache = MemCache.new(servers,o)
end

Instance Method Details

#key(userid, apikey, scope, name, args) ⇒ Object

Returns the memcached key for a given set of args. Does not use the full API key string as the risk of a collision is astronomically high.



12
13
14
# File 'lib/eaal/cache/memcached.rb', line 12

def key(userid, apikey, scope, name, args)
  "#{userid}#{apikey[0..25]}#{scope}#{name}#{args}"
end

#load(userid, apikey, scope, name, args) ⇒ Object

Loads from the cache if there’s a value for it.



27
28
29
30
# File 'lib/eaal/cache/memcached.rb', line 27

def load(userid, apikey, scope, name, args)
  k = key(userid, apikey, scope, name, args)
  ($cache.get(k) or false) rescue false
end

#save(userid, apikey, scope, name, args, xml) ⇒ Object

Saves to cache. It is worth noting that memcached handles expiry for us unlike FileCache as a result, there is no requirement for a validate_cache method- we just get MC to expire the key when we can go get a new copy.



18
19
20
21
22
23
24
# File 'lib/eaal/cache/memcached.rb', line 18

def save(userid, apikey, scope, name, args, xml)
  k = key(userid, apikey, scope, name, args)
  cached_until = Time.parse(xml.match(/<cachedUntil>(.+)<\/cachedUntil>/)[1])
  expires_in = (name=='WalletJournal' ? cached_until.to_i+3600 : cached_until.to_i )
  $cache.delete(k)
  $cache.add(k,xml,expires_in)
end