class Merb::MemorySessionStore

Used for handling multiple sessions stored in memory.

Public Class Methods

new(ttl=nil) click to toggle source

Parameters

ttl<Fixnum>

Session validity time in seconds. Defaults to 1 hour.

:api: private

# File lib/merb-core/dispatch/session/memory.rb, line 44
def initialize(ttl=nil)
  @sessions = Hash.new
  @timestamps = Hash.new
  @mutex = Mutex.new
  @session_ttl = ttl || Merb::Const::HOUR # defaults 1 hour
  start_timer
end

Public Instance Methods

delete_session(session_id) click to toggle source

Parameters

session_id<String>

ID of the session to delete.

:api: private

# File lib/merb-core/dispatch/session/memory.rb, line 82
def delete_session(session_id)
  @mutex.synchronize {
    @timestamps.delete(session_id)
    @sessions.delete(session_id)
  }
end
reap_expired_sessions() click to toggle source

Deletes any sessions that have reached their maximum validity.

:api: private

# File lib/merb-core/dispatch/session/memory.rb, line 92
def reap_expired_sessions
  @timestamps.each do |session_id,stamp|
    delete_session(session_id) if (stamp + @session_ttl) < Time.now 
  end
  GC.start
end
retrieve_session(session_id) click to toggle source

Parameters

session_id<String>

ID of the session to retrieve.

Returns

ContainerSession

The session corresponding to the ID.

:api: private

# File lib/merb-core/dispatch/session/memory.rb, line 59
def retrieve_session(session_id)
  @mutex.synchronize {
    @timestamps[session_id] = Time.now
    @sessions[session_id]
  }
end
start_timer() click to toggle source

Starts the timer that will eventually reap outdated sessions.

:api: private

# File lib/merb-core/dispatch/session/memory.rb, line 102
def start_timer
  Thread.new do
    loop {
      sleep @session_ttl
      reap_expired_sessions
    } 
  end  
end
store_session(session_id, data) click to toggle source

Parameters

session_id<String>

ID of the session to set.

data<ContainerSession>

The session to set.

:api: private

# File lib/merb-core/dispatch/session/memory.rb, line 71
def store_session(session_id, data)
  @mutex.synchronize {
    @timestamps[session_id] = Time.now
    @sessions[session_id] = data
  }
end