lib/plezi/handlers/session.rb in plezi-0.10.17 vs lib/plezi/handlers/session.rb in plezi-0.11.0

- old
+ new

@@ -3,13 +3,12 @@ module SessionStorage module_function # returns a session object def fetch id return Plezi::Session.new(id) if Plezi.redis # avoid a local cache if Redis is used. - @session_cache[id] || (@session_cache[id] = Plezi::Session.new(id)) + GRHttp::Base::FileSessionStorage.fetch id # use the tmp-file-session logic if Redis isn't present end - @session_cache = {} end end # A hash like interface for storing request session data. # The store must implement: store(key, value) (aliased as []=); # fetch(key, default = nil) (aliased as []); @@ -20,25 +19,25 @@ SESSION_LIFETIME = 432_000 # called by the Plezi framework to initiate a session with the id requested def initialize id @id = id if id && (conn=Plezi.redis) - @data=conn.hgetall(id) + return self end - @data ||= {} + failed end # Get a key from the session data store. If a Redis server is supplied, it will be used to synchronize session data. # # Due to scaling considirations, all keys will be converted to strings, so that `"name" == :name` and `1234 == "1234"`. # If you store two keys that evaluate as the same string, they WILL override each other. def [] key key = key.to_s if conn=Plezi.redis conn.expire @id, SESSION_LIFETIME - @data[key] = conn.hget @id, key + return conn.hget @id, key end - @data[key] + failed end alias :fetch :[] # Stores a key in the session's data store. If a Redis server is supplied, it will be used to synchronize session data. # @@ -47,39 +46,49 @@ def []= key, value key = key.to_s if (conn=Plezi.redis) conn.hset @id, key, value conn.expire @id, SESSION_LIFETIME + return value end - @data[key] = value + failed end alias :store :[]= # @return [Hash] returns a shallow copy of the current session data as a Hash. def to_h if (conn=Plezi.redis) conn.expire @id, SESSION_LIFETIME - return (@data=conn.hgetall(@id)).dup + return conn.hgetall(@id) end - @data.dup + failed end # Removes a key from the session's data store. def delete key key = key.to_s if (conn=Plezi.redis) conn.expire @id, SESSION_LIFETIME + ret = conn.hget @id, key conn.hdel @id, key + return ret end - @data.delete key + failed end # Clears the session's data. def clear if (conn=Plezi.redis) - conn.del @id + return conn.del @id end - @data.clear + failed + end + + protected + + def failed + raise 'Redis connection failed while using Redis Session Storage.' + end end GRHttp::SessionManager.storage = Plezi::Base::SessionStorage end