Class: UzuUzu::RackSession::Memcache
- Inherits:
-
Rack::Session::Abstract::ID
- Object
- Rack::Session::Abstract::ID
- UzuUzu::RackSession::Memcache
- Defined in:
- lib/uzuuzu-core/rack_session/memcache.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
Instance Method Summary collapse
- #app(app) ⇒ Object
- #destroy_session(env, session_id, options) ⇒ Object
- #generate_sid ⇒ Object
- #get_session(env, sid) ⇒ Object
-
#initialize(env = nil) ⇒ Memcache
constructor
A new instance of Memcache.
- #set_session(env, session_id, new_session, options) ⇒ Object
- #with_lock(env, default = nil) ⇒ Object
Constructor Details
#initialize(env = nil) ⇒ Memcache
Returns a new instance of Memcache.
13 14 15 16 17 18 |
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 13 def initialize(env=nil) @adapter = :memcache super(nil, env) @mutex = Mutex.new @pool = ::UzuUzu::Wrapper::Wrapper.new('memcache', env['config']).wrapper end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
8 9 10 |
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 8 def adapter @adapter end |
Instance Method Details
#app(app) ⇒ Object
20 21 22 |
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 20 def app(app) @app = app end |
#destroy_session(env, session_id, options) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 50 def destroy_session(env, session_id, ) with_lock(env) do @pool.delete(session_id) generate_sid unless [:drop] end end |
#generate_sid ⇒ Object
24 25 26 27 28 29 |
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 24 def generate_sid loop do sid = super break sid unless @pool.get(sid) end end |
#get_session(env, sid) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 31 def get_session(env, sid) with_lock(env, [nil, {}]) do unless sid and session = @pool.get(sid) sid, session = generate_sid, {} unless /^STORED/ =~ @pool.add(sid, session) raise "Session collision on '#{sid.inspect}'" end end [sid, session] end end |
#set_session(env, session_id, new_session, options) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 43 def set_session(env, session_id, new_session, ) with_lock(env, false) do @pool.set session_id, new_session session_id end end |
#with_lock(env, default = nil) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 57 def with_lock(env, default=nil) @mutex.lock if env['rack.multithread'] yield rescue default ensure @mutex.unlock if @mutex.locked? end |