Class: Rack::Session::Mongo

Inherits:
Abstract::ID
  • Object
show all
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

Instance Method Summary collapse

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, options={})
  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

#connectionObject (readonly)

:nodoc:



11
12
13
# File 'lib/mongo_rack.rb', line 11

def connection
  @connection
end

#dbObject (readonly)

:nodoc:



11
12
13
# File 'lib/mongo_rack.rb', line 11

def db
  @db
end

#mutexObject (readonly)

:nodoc:



11
12
13
# File 'lib/mongo_rack.rb', line 11

def mutex
  @mutex
end

#sessionsObject (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, options )
  return _set_session( env, sid, new_session, options ) unless env['rack.multithread']
  mutex.synchronize do    
    return _set_session( env, sid, new_session, options )
  end        
end