Created
May 9, 2009 18:02
-
-
Save ctdk/109361 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
require "memcached" | |
rescue LoadError | |
puts "You need the memcached (the libmemcached client, not the other one) gem to use the Memcached moneta store" | |
exit | |
end | |
# uses the memcached gem properly, and traps the weirdass exception a get on an undefined key throws. May be worth changing | |
# further to allow the extra flags memcached lets you use, like marshal => true|false or the various memcached flags | |
module Moneta | |
class MemCached | |
include Defaults | |
def initialize(options = {}) | |
@cache = Memcached.new(options.delete(:server), options) | |
end | |
def key?(key) | |
!self[key].nil? | |
end | |
alias has_key? key? | |
def [](key) | |
begin | |
val = @cache.get(key) | |
rescue | |
val = nil | |
end | |
return val | |
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