Skip to content

Instantly share code, notes, and snippets.

@ctdk
Created May 10, 2009 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ctdk/109499 to your computer and use it in GitHub Desktop.
Save ctdk/109499 to your computer and use it in GitHub Desktop.
begin
require "memcached"
rescue LoadError
require "memcache"
rescue LoadError
puts "You need the memcache or the memcached gem to use the Memcache moneta store"
exit
end
module Moneta
class Memcache
include Defaults
def initialize(options = {})
@cache = defined?(MemCache) ? MemCache.new(options.delete(:server), options) : Memcached.new(options.delete(:server), options)
end
def key?(key)
!self[key].nil?
end
alias has_key? key?
def [](key)
begin
@cache.get(key)
rescue Memcached::NotFound
nil
end
end
def []=(key, value)
store(key, value)
end
def delete(key)
value = self[key]
@cache.delete(key) if value
value
end
def store(key, value, options = {})
args = [key, value, options[:expires_in]].compact
@cache.set(*args)
end
def update_key(key, options = {})
val = self[key]
self.store(key, val, options)
end
def clear
@cache.flush_all
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment