Created
July 17, 2010 04:00
-
-
Save tylr/479222 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
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