1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
class Redis
class Store < self
module Ttl
def set(key, value, options = nil)
if ttl = expires_in(options)
setex(key, ttl.to_i, value, :raw => true)
else
super(key, value)
end
end
def setnx(key, value, options = nil)
if ttl = expires_in(options)
setnx_with_expire(key, value, ttl.to_i)
else
super(key, value)
end
end
protected
def setnx_with_expire(key, value, ttl)
multi do
setnx(key, value, :raw => true)
expire(key, ttl)
end
end
private
def expires_in(options)
if options
# Rack::Session Merb Rails/Sinatra
options[:expire_after] || options[:expires_in] || options[:expire_in]
end
end
end
end
end
|