Skip to content

Instantly share code, notes, and snippets.

@tylr
Created July 17, 2010 04:00
Show Gist options
  • Save tylr/479222 to your computer and use it in GitHub Desktop.
Save tylr/479222 to your computer and use it in GitHub Desktop.
module ShitCan
require 'memcached'
def self.load(con_path='localhost:11211')
@cache = Memcached.new(con_path)
end
def self.cache
if @cache
return @cache
else
raise ShitCan, 'NO CAN HERE!'
end
end
def self.set key, value=nil, ttl=600
value = yield if value.nil?
cache.set key, value
end
def self.get *args
cache.get *args
end
def self.get_or_set key, value=nil
if val = exists?(key)
val
else
set key, (value || yield)
end
end
def self.skip_if_exists key
yield unless exists? key
end
def self.exists? key
begin
return get key
rescue
return false
end
end
end
SHITCAN ? ShitCan.load("#{SHITCAN[:path]}:#{SHITCAN[:port]}") : ShitCan.load
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment