Skip to content

Instantly share code, notes, and snippets.

@ab5tract
Created September 8, 2008 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ab5tract/9347 to your computer and use it in GitHub Desktop.
Save ab5tract/9347 to your computer and use it in GitHub Desktop.
# All @memcached's are now Waves.cache ... will this be a problem when in reality
# Waves.cache = Waves::Layers::Cache::MemcachedCache.new("localhost:11211") ?
# It's the self referentiality that is confusing me....
module Waves
module Layers
module Cache
class MemcachedCache < Waves::Cache
require 'memcached'
def initialize(servers, opt = {})
# Waves::Layers::Cache::MemcachedCache.new is the same format as Memcached.new
Waves.cache = Memcached.new(servers, opt)
end
def add(key,value, ttl = 0, marshal = true)
Waves.cache.add(key.to_s,value,ttl,marshal)
end
def get(key)
Waves.cache.get(key.to_s)
rescue Memcached::NotFound # In order to keep the MemcachedCache layer compliant with Waves::Cache...
return nil # ...we need to be able to expect that an absent key returns 'nil'.
end
def delete(*keys)
keys.each {|key| Waves.cache.delete(key.to_s) }
end
def clear
Waves.cache.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)
Waves.cache.__send__(*args, &block)
rescue => e
Waves::Logger.error e.to_s
nil
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment