class Rack::Session::Pool - rack-1.6.4 Documentation

# class Rack::Session::Pool

Rack::Session::Pool provides simple cookie based session management. Session data is stored in a hash held by @pool. In the context of a multithreaded environment, sessions being committed to the pool is done in a merging manner.

The :drop option is available in rack.session.options if you wish to explicitly remove the session from the session cache.

Example:

    myapp = MyRackApp.new
    sessioned = Rack::Session::Pool.new(myapp,
      :domain => 'foo.com',
      :expire_after => 2592000
    )
    Rack::Handler::WEBrick.run sessioned

### Constants

DEFAULT_OPTIONS

### Attributes

mutex[R]
pool[R]

### Public Class Methods

new(app, options={}) click to toggle source
Calls superclass method Rack::Session::Abstract::ID.new

    # File lib/rack/session/pool.rb, line 31
    def initialize(app, options={})
      super
      @pool = Hash.new
      @mutex = Mutex.new
    end

### Public Instance Methods

destroy_session(env, session_id, options) click to toggle source

    # File lib/rack/session/pool.rb, line 61
    def destroy_session(env, session_id, options)
      with_lock(env) do
        @pool.delete(session_id)
        generate_sid unless options[:drop]
      end
    end

generate_sid() click to toggle source
Calls superclass method Rack::Session::Abstract::ID#generate_sid

    # File lib/rack/session/pool.rb, line 37
    def generate_sid
      loop do
        sid = super
        break sid unless @pool.key? sid
      end
    end

get_session(env, sid) click to toggle source

    # File lib/rack/session/pool.rb, line 44
    def get_session(env, sid)
      with_lock(env) do
        unless sid and session = @pool[sid]
          sid, session = generate_sid, {}
          @pool.store sid, session
        end
        [sid, session]
      end
    end

set_session(env, session_id, new_session, options) click to toggle source

    # File lib/rack/session/pool.rb, line 54
    def set_session(env, session_id, new_session, options)
      with_lock(env) do
        @pool.store session_id, new_session
        session_id
      end
    end

with_lock(env) { || ... } click to toggle source

    # File lib/rack/session/pool.rb, line 68
    def with_lock(env)
      @mutex.lock if env['rack.multithread']
      yield
    ensure
      @mutex.unlock if @mutex.locked?
    end