Class: Blix::RedisStore
- Inherits:
-
Object
- Object
- Blix::RedisStore
- Defined in:
- lib/blix/utils/redis_store.rb
Constant Summary collapse
- STORE_PREFIX =
'session'
Instance Attribute Summary collapse
-
#_opts ⇒ Object
readonly
Returns the value of attribute _opts.
-
#_prefix ⇒ Object
readonly
Returns the value of attribute _prefix.
Instance Method Summary collapse
- #_all_keys(name = nil) ⇒ Object
- #_decode(msg) ⇒ Object
- #_encode(data) ⇒ Object
- #_key(name) ⇒ Object
- #cleaning? ⇒ Boolean
-
#cleanup(opts = nil) ⇒ Object
delete expired sessions from the store.
-
#delete_data(id) ⇒ Object
delete a record from the store.
-
#delete_session(id) ⇒ Object
delete a seession from the store.
-
#get_data(id) ⇒ Object
retrieve raw data and reset the expire time.
-
#get_hash(id) ⇒ Object
if decoding does not succeed then delete the data and return nil.
-
#get_session(id, opts = {}) ⇒ Object
retrieve a session hash.
-
#initialize(opts = {}) ⇒ RedisStore
constructor
A new instance of RedisStore.
-
#length ⇒ Object
the number of sessions in the store.
-
#redis ⇒ Object
the redis session store.
-
#reset(name = nil) ⇒ Object
remove all sessions from the store.
-
#run_cleanup_thread(opts = nil) ⇒ Object
redis takes care of this operation.
-
#stop_cleanup_thread(_opts = nil) ⇒ Object
redis takes care of this operation.
-
#store_data(id, data) ⇒ Object
store raw data.
- #store_hash(id, hash) ⇒ Object
-
#store_session(id, hash) ⇒ Object
store a session hash.
Constructor Details
#initialize(opts = {}) ⇒ RedisStore
Returns a new instance of RedisStore.
18 19 20 21 22 23 |
# 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 |
Instance Attribute Details
#_opts ⇒ Object (readonly)
Returns the value of attribute _opts.
14 15 16 |
# File 'lib/blix/utils/redis_store.rb', line 14 def _opts @_opts end |
#_prefix ⇒ Object (readonly)
Returns the value of attribute _prefix.
14 15 16 |
# File 'lib/blix/utils/redis_store.rb', line 14 def _prefix @_prefix end |
Instance Method Details
#_all_keys(name = nil) ⇒ Object
115 116 117 |
# File 'lib/blix/utils/redis_store.rb', line 115 def _all_keys(name=nil) redis.keys("#{_prefix}#{name}*") || [] end |
#_decode(msg) ⇒ Object
146 147 148 |
# File 'lib/blix/utils/redis_store.rb', line 146 def _decode(msg) Marshal.load(msg) end |
#_encode(data) ⇒ Object
142 143 144 |
# File 'lib/blix/utils/redis_store.rb', line 142 def _encode(data) Marshal.dump(data) end |
#_key(name) ⇒ Object
101 102 103 |
# File 'lib/blix/utils/redis_store.rb', line 101 def _key(name) _prefix + name end |
#cleaning? ⇒ Boolean
156 157 158 |
# File 'lib/blix/utils/redis_store.rb', line 156 def cleaning? false end |
#cleanup(opts = nil) ⇒ Object
delete expired sessions from the store. this should be handled automatically by redis if the ttl is set on save correctly
140 |
# File 'lib/blix/utils/redis_store.rb', line 140 def cleanup(opts = nil); end |
#delete_data(id) ⇒ Object
delete a record from the store
44 45 46 |
# File 'lib/blix/utils/redis_store.rb', line 44 def delete_data(id) redis.del(_key(id)) end |
#delete_session(id) ⇒ Object
delete a seession from the store
97 98 99 |
# File 'lib/blix/utils/redis_store.rb', line 97 def delete_session(id) redis.del(_key(id)) end |
#get_data(id) ⇒ Object
retrieve raw data and reset the expire time.
28 29 30 31 32 33 |
# 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) ⇒ Object
if decoding does not succeed then delete the data and return nil.
50 51 52 53 54 55 56 57 58 |
# 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 = {}) ⇒ Object
retrieve a session hash.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# 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 ⇒ Object
the number of sessions in the store
134 135 136 |
# File 'lib/blix/utils/redis_store.rb', line 134 def length _all_keys.length end |
#redis ⇒ Object
the redis session store
120 121 122 123 124 125 126 127 128 129 130 131 |
# 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) ⇒ Object
remove all sessions from the store
110 111 112 113 |
# 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) ⇒ Object
redis takes care of this operation
151 |
# File 'lib/blix/utils/redis_store.rb', line 151 def run_cleanup_thread(opts = nil); end |
#stop_cleanup_thread(_opts = nil) ⇒ Object
redis takes care of this operation
154 |
# File 'lib/blix/utils/redis_store.rb', line 154 def stop_cleanup_thread(_opts = nil); end |
#store_data(id, data) ⇒ Object
store raw data
36 37 38 39 40 41 |
# 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) ⇒ Object
60 61 62 63 |
# 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) ⇒ Object
store a session hash
87 88 89 90 91 92 93 94 |
# 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 |