Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# mostly golden
module Waves
module Layers
module Cache
class Memcached < Waves::Cache
require 'memcached'
def initialize(args)
raise ArgumentError, "need :servers to not be nil" if args[:servers].nil?
args[:opt] = args[:opt].nil? ? {} : args[:opt]
@memcached = ::Memcached.new(args[:servers], args[:opt])
end
def add(key,value, ttl = 0, marshal = true)
@memcached.add(key.to_s,value,ttl,marshal)
end
def get(key)
@memcached.get(key.to_s)
rescue ::Memcached::NotFound # In order to keep the MemcachedCache layer compliant with Waves::Cache...
# ...we need to be able to expect that an absent key raises WavesCacheError::KeyMissing
raise WavesCacheError::KeyMissing, "#{key} doesn't exist"
end
def delete(*keys)
keys.each {|key| @memcached.delete(key.to_s) }
end
def clear
@memcached.flush
end
alias_method :store, :add # Override our natural Waves::Cache :store method with Memcache's :add
alias_method :fetch, :get # Override our natural Waves::Cache :fetch method with Memcache's :get
def method_missing(*args, &block)
@memcached.__send__(*args, &block)
rescue => e
Waves::Logger.error e.to_s
nil
end
# Would this work?? I'm assuming this block gets called the 'moment' the layer is included?
def self.included(app)
Waves.cache = Waves::Layers::Cache::Memcached.new( Waves.config.cache )
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment