Class: Rack::Session::LevelDB
- Inherits:
-
Abstract::ID
- Object
- Abstract::ID
- Rack::Session::LevelDB
- Defined in:
- lib/rack/session/leveldb.rb
Constant Summary collapse
- VERSION =
'0.0.1'
- DEFAULT_OPTIONS =
Abstract::ID::DEFAULT_OPTIONS.merge \ :namespace => 'rack.session:', :db_path => "#{ENV['TMP'] || '/tmp'}/rack.session", :cleanup => true
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
- #cleanup_expired(time = Time.now) ⇒ Object
- #destroy_session(env, session_id, options) ⇒ Object
- #generate_sid ⇒ Object
- #get_session(env, sid) ⇒ Object
-
#initialize(app, options = {}) ⇒ LevelDB
constructor
A new instance of LevelDB.
- #set_session(env, session_id, new_session, options) ⇒ Object
- #with_lock(env, default = nil) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ LevelDB
Returns a new instance of LevelDB.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rack/session/leveldb.rb', line 15 def initialize(app, ={}) = {:db_path => } if .is_a? String = {:cache => } if .is_a? ::LevelDB::DB super if [:cache] if [:cache].kind_of? ::LevelDB::DB @pool = [:cache] else raise ":cache is not LevelDB" end else @pool = ::LevelDB::DB.new @default_options[:db_path] end cleanup_expired if @default_options[:cleanup] @mutex = Mutex.new end |
Instance Attribute Details
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
8 9 10 |
# File 'lib/rack/session/leveldb.rb', line 8 def mutex @mutex end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
8 9 10 |
# File 'lib/rack/session/leveldb.rb', line 8 def pool @pool end |
Instance Method Details
#cleanup_expired(time = Time.now) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/rack/session/leveldb.rb', line 76 def cleanup_expired(time = Time.now) return unless @default_options[:expire_after] @pool.each do |k, v| if k.start_with?(@default_options[:namespace]) and time - Marshal.load(v)[:datetime] > @default_options[:expire_after] @pool.delete(k) end end end |
#destroy_session(env, session_id, options) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/rack/session/leveldb.rb', line 57 def destroy_session(env, session_id, ) with_lock(env) do _delete(session_id) generate_sid unless [:drop] end end |
#generate_sid ⇒ Object
33 34 35 36 37 38 |
# File 'lib/rack/session/leveldb.rb', line 33 def generate_sid loop do sid = super break sid unless _exists? sid end end |
#get_session(env, sid) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/rack/session/leveldb.rb', line 40 def get_session(env, sid) with_lock(env, [nil, {}]) do unless sid and session = _get(sid) sid, session = generate_sid, {} _put sid, session end [sid, session] end end |
#set_session(env, session_id, new_session, options) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/rack/session/leveldb.rb', line 50 def set_session(env, session_id, new_session, ) with_lock(env, false) do _put session_id, new_session session_id end end |
#with_lock(env, default = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rack/session/leveldb.rb', line 64 def with_lock(env, default=nil) @mutex.lock if env['rack.multithread'] yield rescue warn "#{self} is unable to open #{@default_options[:db_path]}." warn $!.inspect default ensure @mutex.unlock if @mutex.locked? end |