Class: Rack::Session::AbstractDBM
- Inherits:
-
Abstract::ID
- Object
- Abstract::ID
- Rack::Session::AbstractDBM
- Defined in:
- lib/rack/session/abstract_dbm.rb
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, path = nil, options = {}) ⇒ AbstractDBM
constructor
A new instance of AbstractDBM.
- #set_session(env, session_id, new_session, options) ⇒ Object
- #with_lock(env, default = nil) ⇒ Object
Constructor Details
#initialize(app, path = nil, options = {}) ⇒ AbstractDBM
Returns a new instance of AbstractDBM.
8 9 10 11 12 13 14 15 16 |
# File 'lib/rack/session/abstract_dbm.rb', line 8 def initialize(app, path=nil, ={}) super(app, ) @mutex = Mutex.new db_path = path || (ENV['RACK_ROOT'] ? ::File.join(ENV['RACK_ROOT'], 'tmp', default_session_file) : @default_options[:dbm_path]) opts = @default_options @pool = dbklass.new(db_path) end |
Instance Attribute Details
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
6 7 8 |
# File 'lib/rack/session/abstract_dbm.rb', line 6 def mutex @mutex end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
6 7 8 |
# File 'lib/rack/session/abstract_dbm.rb', line 6 def pool @pool end |
Instance Method Details
#destroy_session(env, session_id, options) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/rack/session/abstract_dbm.rb', line 46 def destroy_session(env, session_id, ) with_lock(env) do @pool.delete(session_id) generate_sid unless [:drop] end end |
#generate_sid ⇒ Object
18 19 20 21 22 23 |
# File 'lib/rack/session/abstract_dbm.rb', line 18 def generate_sid loop do sid = super break sid unless @pool[sid] end end |
#get_session(env, sid) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rack/session/abstract_dbm.rb', line 25 def get_session(env, sid) with_lock(env, [nil, {}]) do unless sid and session = Marshal.load(@pool[sid] || "\x04\b0") sid, session = generate_sid, {} begin @pool[sid] = Marshal.dump(session) rescue raise "Session collision on '#{sid.inspect}'" end end [sid, session] end end |
#set_session(env, session_id, new_session, options) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/rack/session/abstract_dbm.rb', line 39 def set_session(env, session_id, new_session, ) with_lock(env, false) do @pool[session_id] = Marshal.dump(new_session) session_id end end |
#with_lock(env, default = nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rack/session/abstract_dbm.rb', line 53 def with_lock(env, default=nil) @mutex.lock if env['rack.multithread'] yield rescue StandardError if $VERBOSE warn "#{self} is unable to write to dbm." warn $!.inspect end default ensure @mutex.unlock if @mutex.locked? end |