class Blix::RedisStore
Constants
- STORE_PREFIX
Attributes
_opts[R]
_prefix[R]
Public Class Methods
new(opts = {})
click to toggle source
# File lib/blix/utils/redis_store.rb, line 18 def initialize(opts = {}) @_opts = ::Blix::Rest::StringHash.new @_opts[:prefix] = STORE_PREFIX @_opts.merge!(opts) @_prefix = _opts[:prefix] end
Public Instance Methods
_all_keys(name=nil)
click to toggle source
# File lib/blix/utils/redis_store.rb, line 115 def _all_keys(name=nil) redis.keys("#{_prefix}#{name}*") || [] end
_decode(msg)
click to toggle source
# File lib/blix/utils/redis_store.rb, line 146 def _decode(msg) Marshal.load(msg) end
_encode(data)
click to toggle source
# File lib/blix/utils/redis_store.rb, line 142 def _encode(data) Marshal.dump(data) end
_key(name)
click to toggle source
# File lib/blix/utils/redis_store.rb, line 101 def _key(name) _prefix + name end
cleaning?()
click to toggle source
# File lib/blix/utils/redis_store.rb, line 156 def cleaning? false end
cleanup(opts = nil)
click to toggle source
delete expired sessions from the store. this should be handled automatically by redis if the ttl is set on save correctly
# File lib/blix/utils/redis_store.rb, line 140 def cleanup(opts = nil); end
delete_data(id)
click to toggle source
delete a record from the store
# File lib/blix/utils/redis_store.rb, line 44 def delete_data(id) redis.del(_key(id)) end
delete_session(id)
click to toggle source
delete a seession from the store
# File lib/blix/utils/redis_store.rb, line 97 def delete_session(id) redis.del(_key(id)) end
get_data(id)
click to toggle source
retrieve raw data and reset the expire time.
# File lib/blix/utils/redis_store.rb, line 28 def get_data(id) k = _key(id) data = redis.get(k) redis.expire(k, _opts[:expire_secs]) if data && _opts[:reset_expire_on_get] && _opts.key?(:expire_secs) data end
get_hash(id)
click to toggle source
if decoding does not succeed then delete the data and return nil.
# File lib/blix/utils/redis_store.rb, line 50 def get_hash(id) str = get_data(id) str && begin _decode(str) rescue Exception =>e delete_data(id) nil end end
get_session(id,opts={})
click to toggle source
retrieve a session hash.
# File lib/blix/utils/redis_store.rb, line 66 def get_session(id,opts={}) str = redis.get(_key(id)) opts = ::Blix::Rest::StringHash.new.merge(opts) hash = begin str && ::Blix::Rest::StringHash.new(_decode(str)) rescue redis.del(_key(id)) hash = nil end if hash && (min_time = get_expiry_time(opts)) && (hash['_last_access'] < min_time) delete_session(id) raise SessionExpiredError end raise SessionExpiredError if !hash && opts[:nocreate] hash ||= ::Blix::Rest::StringHash.new hash['_last_access'] = Time.now hash end
length()
click to toggle source
the number of sessions in the store
# File lib/blix/utils/redis_store.rb, line 134 def length _all_keys.length end
redis()
click to toggle source
the redis session store
# File lib/blix/utils/redis_store.rb, line 120 def redis @redis ||= begin r = Redis.new begin r.ping rescue Exception => e Blix::Rest.logger.error "cannot reach redis server:#{e}" raise end r end end
reset(name=nil)
click to toggle source
remove all sessions from the store
# File lib/blix/utils/redis_store.rb, line 110 def reset(name=nil) keys = _all_keys(name) redis.del(*keys) unless keys.empty? end
run_cleanup_thread(opts = nil)
click to toggle source
redis takes care of this operation
# File lib/blix/utils/redis_store.rb, line 151 def run_cleanup_thread(opts = nil); end
stop_cleanup_thread(_opts = nil)
click to toggle source
redis takes care of this operation
# File lib/blix/utils/redis_store.rb, line 154 def stop_cleanup_thread(_opts = nil); end
store_data(id, data)
click to toggle source
store raw data
# File lib/blix/utils/redis_store.rb, line 36 def store_data(id, data) params = {} params[:ex] = _opts[:expire_secs] if _opts.key?(:expire_secs) redis.set(_key(id), data, params) data end
store_hash(id,hash)
click to toggle source
# File lib/blix/utils/redis_store.rb, line 60 def store_hash(id,hash) store_data(id, _encode(hash || {})) hash end
store_session(id, hash)
click to toggle source
store a session hash
# File lib/blix/utils/redis_store.rb, line 87 def store_session(id, hash) params = {} params[:ex] = _opts[:expire_secs] if _opts.key?(:expire_secs) hash ||= {} hash['_last_access'] = Time.now redis.set(_key(id), _encode(hash), params) hash end
Private Instance Methods
get_expiry_secs(opts)
click to toggle source
# File lib/blix/utils/redis_store.rb, line 168 def get_expiry_secs(opts) opts[:expire_secs] || opts['expire_secs'] end
get_expiry_time(opts)
click to toggle source
# File lib/blix/utils/redis_store.rb, line 162 def get_expiry_time(opts) if expire = opts[:expire_secs] || opts['expire_secs'] Time.now - expire end end