Class: Rack::Session::RethinkDB
- Inherits:
-
Abstract::ID
- Object
- Abstract::ID
- Rack::Session::RethinkDB
- Defined in:
- lib/rack/session/rethinkdb.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
Abstract::ID::DEFAULT_OPTIONS.merge \ port: 28_015, table: 'sessions'
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #destroy_session(env, session_id, options) ⇒ Object
- #generate_sid ⇒ Object
- #get_session(env, sid) ⇒ Object
-
#initialize(app, options = {}) ⇒ RethinkDB
constructor
A new instance of RethinkDB.
- #set_session(env, session_id, new_session, _options) ⇒ Object
- #with_lock(env, default = nil) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ RethinkDB
Returns a new instance of RethinkDB.
25 26 27 28 29 30 31 32 33 |
# File 'lib/rack/session/rethinkdb.rb', line 25 def initialize(app, = {}) super @host = [:host] @port = @default_options[:port] @db = @default_options[:db] @table = @default_options[:table] @mutex = Mutex.new end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
13 14 15 |
# File 'lib/rack/session/rethinkdb.rb', line 13 def db @db end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
13 14 15 |
# File 'lib/rack/session/rethinkdb.rb', line 13 def host @host end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
13 14 15 |
# File 'lib/rack/session/rethinkdb.rb', line 13 def mutex @mutex end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
13 14 15 |
# File 'lib/rack/session/rethinkdb.rb', line 13 def pool @pool end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
13 14 15 |
# File 'lib/rack/session/rethinkdb.rb', line 13 def port @port end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
13 14 15 |
# File 'lib/rack/session/rethinkdb.rb', line 13 def table @table end |
Instance Method Details
#destroy_session(env, session_id, options) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/rack/session/rethinkdb.rb', line 60 def destroy_session(env, session_id, ) with_lock(env) do # @pool.del(session_id) ::RethinkDB::RQL.new.db(db).table(table).get(session_id).delete .run(connection) generate_sid unless [:drop] end end |
#generate_sid ⇒ Object
35 36 37 38 39 40 |
# File 'lib/rack/session/rethinkdb.rb', line 35 def generate_sid loop do sid = super break sid unless _exists?(sid) end end |
#get_session(env, sid) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rack/session/rethinkdb.rb', line 42 def get_session(env, sid) with_lock(env, [nil, {}]) do unless sid && (session = _get(sid)) sid, session = generate_sid, {} _put(sid, session) end [sid, session] end end |
#set_session(env, session_id, new_session, _options) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/rack/session/rethinkdb.rb', line 53 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
69 70 71 72 73 74 75 76 |
# File 'lib/rack/session/rethinkdb.rb', line 69 def with_lock(env, default = nil) mutex.lock if env['rack.multithread'] yield rescue default ensure mutex.unlock if mutex.locked? end |