class EAAL::Cache::MemcachedCache
Public Class Methods
new(servers="localhost:11211",options={})
click to toggle source
# 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
Public Instance Methods
key(userid, apikey, scope, name, args)
click to toggle source
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.
# 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)
click to toggle source
Loads from the cache if there’s a value for it.
# 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)
click to toggle source
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.
# 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