Class: Rack::Session::Dalli
- Inherits:
-
Abstract::ID
- Object
- Abstract::ID
- Rack::Session::Dalli
- Defined in:
- lib/rack/session/dalli.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
Abstract::ID::DEFAULT_OPTIONS.merge \ :namespace => 'rack:session', :memcache_server => 'localhost:11211'
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
-
#initialize(app, options = {}) ⇒ Dalli
constructor
A new instance of Dalli.
- #set_session(env, session_id, new_session, options) ⇒ Object
- #with_lock(env, default = nil) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Dalli
Returns a new instance of Dalli.
13 14 15 16 17 18 19 |
# File 'lib/rack/session/dalli.rb', line 13 def initialize(app, ={}) super @mutex = Mutex.new mserv = @default_options[:memcache_server] mopts = @default_options.reject{|k,v| !DEFAULT_OPTIONS.include? k } @pool = [:cache] || ::Dalli::Client.new(mserv, mopts) end |
Instance Attribute Details
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
7 8 9 |
# File 'lib/rack/session/dalli.rb', line 7 def mutex @mutex end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
7 8 9 |
# File 'lib/rack/session/dalli.rb', line 7 def pool @pool end |
Instance Method Details
#destroy_session(env, session_id, options) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/rack/session/dalli.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
21 22 23 24 25 26 |
# File 'lib/rack/session/dalli.rb', line 21 def generate_sid loop do sid = super break sid unless @pool.get(sid) end end |
#get_session(env, sid) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rack/session/dalli.rb', line 28 def get_session(env, sid) with_lock(env, [nil, {}]) do unless sid and session = @pool.get(sid) sid, session = generate_sid, {} unless @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
40 41 42 43 44 45 46 47 48 |
# File 'lib/rack/session/dalli.rb', line 40 def set_session(env, session_id, new_session, ) expiry = [:expire_after] expiry = expiry.nil? ? 0 : expiry + 1 with_lock(env, false) do @pool.set session_id, new_session, expiry session_id end end |
#with_lock(env, default = nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rack/session/dalli.rb', line 57 def with_lock(env, default=nil) @mutex.lock if env['rack.multithread'] yield rescue ::Dalli::DalliError, Errno::ECONNREFUSED raise if $!. =~ /undefined class/ if $VERBOSE warn "#{self} is unable to find memcached server." warn $!.inspect end default ensure @mutex.unlock if @mutex.locked? end |