Class: RackSessionRedisStore::Session
- Inherits:
-
Rack::Session::Abstract::ID
- Object
- Rack::Session::Abstract::ID
- RackSessionRedisStore::Session
- Defined in:
- lib/rack_session_redis_store.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
::Rack::Session::Abstract::ID::DEFAULT_OPTIONS
Instance Attribute Summary collapse
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
Instance Method Summary collapse
- #destroy_session(env, session_id, options) ⇒ Object
- #generate_sid ⇒ Object
-
#get_session(env, sid) ⇒ Object
Synopsis call at call -> context -> prepare_session -> SessionHash.new in rack/session/abstract/id.rb.
-
#initialize(app, options = {}) ⇒ Session
constructor
A new instance of Session.
-
#set_session(env, session_id, new_session, options) ⇒ Object
Synopsis call at call -> context -> commit_session in rack/session/abstract/id.rb.
- #with_lock(env, default = nil) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Session
Returns a new instance of Session.
28 29 30 31 32 33 34 35 |
# File 'lib/rack_session_redis_store.rb', line 28 def initialize(app, = {}) super @mutex = Mutex.new host = [:host] || '127.0.0.1' port = [:port] || 6379 namespace = [:namespace] || :session @pool = RedisJsonSerializer::Serializer.new(host: host, port: port, namespace: namespace) end |
Instance Attribute Details
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
25 26 27 |
# File 'lib/rack_session_redis_store.rb', line 25 def mutex @mutex end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
25 26 27 |
# File 'lib/rack_session_redis_store.rb', line 25 def pool @pool end |
Instance Method Details
#destroy_session(env, session_id, options) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/rack_session_redis_store.rb', line 79 def destroy_session(env, session_id, ) with_lock(env) do @pool.del session_id generate_sid unless [:drop] end end |
#generate_sid ⇒ Object
37 38 39 40 41 42 |
# File 'lib/rack_session_redis_store.rb', line 37 def generate_sid loop do sid = super break sid unless @pool.get sid end end |
#get_session(env, sid) ⇒ Object
Synopsis
call at call -> context -> prepare_session -> SessionHash.new in rack/session/abstract/id.rb
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rack_session_redis_store.rb', line 47 def get_session(env, sid) with_lock(env, [nil, {}]) do unless sid and session = @pool.get(sid) sid, session = generate_sid, {} unless /^OK/ =~ @pool.set(sid, session) raise "Session collision on '#{sid.inspect}'" end end if session.has_key? :flash session[:flash] = Marshal.load(::Base64.decode64(session[:flash])) end [sid, session] end end |
#set_session(env, session_id, new_session, options) ⇒ Object
Synopsis
call at call -> context -> commit_session in rack/session/abstract/id.rb
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rack_session_redis_store.rb', line 65 def set_session(env, session_id, new_session, ) with_lock(env, false) do if new_session.has_key? 'flash' new_session['flash'] = ::Base64.encode64(Marshal.dump(new_session['flash'])) end if ttl = [:expire_after] @pool.setex session_id, ttl, new_session else @pool.set session_id, new_session end session_id end end |
#with_lock(env, default = nil) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rack_session_redis_store.rb', line 86 def with_lock(env, default=nil) @mutex.lock if env['rack.multithread'] yield rescue Errno::ECONNREFUSED if $VERBOSE warn "#{self} is unable to find Redis server." warn $!.inspect end default ensure @mutex.unlock if @mutex.locked? end |