Class: Rack::Session::Mongo
- Inherits:
-
Abstract::ID
- Object
- Abstract::ID
- Rack::Session::Mongo
- Defined in:
- lib/mongo_rack.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
Options for mongo_rack
- :server
-
Specifies server, port, db and collection location. Defaults to localhost:27017/mongo_session/sessions. Format must conform to the format host:port/database_name/collection_name.
- :pool_size
-
The connection socket pool size - see mongo-ruby-driver docs for settings. Defaults to 1 connection.
- :pool_timeout
-
The connection pool timeout. see mongo-ruby-driver docs for settings. Defaults to 1 sec.
- :logging
-
Set to true to enable logger. Default is false
Abstract::ID::DEFAULT_OPTIONS.merge \ :server => 'localhost:27017/mongo_session/sessions', :pool_size => 1, :pool_timeout => 1.0, :log_level => :error
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
:nodoc:.
-
#db ⇒ Object
readonly
:nodoc:.
-
#mutex ⇒ Object
readonly
:nodoc:.
-
#sessions ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#get_session(env, sid) ⇒ Object
Fetch session with optional session id.
-
#initialize(app, options = {}) ⇒ Mongo
constructor
Initializes mongo_rack.
-
#set_session(env, sid, new_session, options) ⇒ Object
Update session params and sync to mongoDB.
Constructor Details
#initialize(app, options = {}) ⇒ Mongo
Initializes mongo_rack. Pass in options for default override.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mongo_rack.rb', line 33 def initialize(app, ={}) super host, port, db_name, cltn_name = parse_server_desc( @default_options[:server] ) @mutex = Mutex.new @connection = ::Mongo::Connection.new( host, port, :pool_size => @default_options[:pool_size], :timeout => @default_options[:pool_timeout] ) @db = @connection.db( db_name ) @sessions = @db[cltn_name] @logger = ::Logger.new( $stdout ) @logger.level = set_log_level( @default_options[:log_level] ) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
:nodoc:
11 12 13 |
# File 'lib/mongo_rack.rb', line 11 def connection @connection end |
#db ⇒ Object (readonly)
:nodoc:
11 12 13 |
# File 'lib/mongo_rack.rb', line 11 def db @db end |
#mutex ⇒ Object (readonly)
:nodoc:
11 12 13 |
# File 'lib/mongo_rack.rb', line 11 def mutex @mutex end |
#sessions ⇒ Object (readonly)
:nodoc:
11 12 13 |
# File 'lib/mongo_rack.rb', line 11 def sessions @sessions end |
Instance Method Details
#get_session(env, sid) ⇒ Object
Fetch session with optional session id. Retrieve session from mongodb if any
52 53 54 55 56 57 |
# File 'lib/mongo_rack.rb', line 52 def get_session( env, sid ) return _get_session( env, sid ) unless env['rack.multithread'] mutex.synchronize do return _get_session( env, sid ) end end |
#set_session(env, sid, new_session, options) ⇒ Object
Update session params and sync to mongoDB.
60 61 62 63 64 65 |
# File 'lib/mongo_rack.rb', line 60 def set_session( env, sid, new_session, ) return _set_session( env, sid, new_session, ) unless env['rack.multithread'] mutex.synchronize do return _set_session( env, sid, new_session, ) end end |