Class: Merb::MemorySessionStore

Inherits:
Object
  • Object
show all
Defined in:
lib/merb-core/dispatch/session/memory.rb

Overview

Used for handling multiple sessions stored in memory.

Instance Method Summary collapse

Constructor Details

#initialize(ttl = nil) ⇒ MemorySessionStore

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters

ttl<Fixnum>

Session validity time in seconds. Defaults to 1 hour.



42
43
44
45
46
47
48
# File 'lib/merb-core/dispatch/session/memory.rb', line 42

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

Instance Method Details

#delete_session(session_id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters

session_id<String>

ID of the session to delete.



80
81
82
83
84
85
# File 'lib/merb-core/dispatch/session/memory.rb', line 80

def delete_session(session_id)
  @mutex.synchronize {
    @timestamps.delete(session_id)
    @sessions.delete(session_id)
  }
end

#reap_expired_sessionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deletes any sessions that have reached their maximum validity.



90
91
92
93
94
95
# File 'lib/merb-core/dispatch/session/memory.rb', line 90

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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters

session_id<String>

ID of the session to retrieve.

Returns

ContainerSession

The session corresponding to the ID.



57
58
59
60
61
62
# File 'lib/merb-core/dispatch/session/memory.rb', line 57

def retrieve_session(session_id)
  @mutex.synchronize {
    @timestamps[session_id] = Time.now
    @sessions[session_id]
  }
end

#start_timerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Starts the timer that will eventually reap outdated sessions.



100
101
102
103
104
105
106
107
# File 'lib/merb-core/dispatch/session/memory.rb', line 100

def start_timer
  Thread.new do
    loop {
      sleep @session_ttl
      reap_expired_sessions
    } 
  end  
end

#store_session(session_id, data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters

session_id<String>

ID of the session to set.

data<ContainerSession>

The session to set.



69
70
71
72
73
74
# File 'lib/merb-core/dispatch/session/memory.rb', line 69

def store_session(session_id, data)
  @mutex.synchronize {
    @timestamps[session_id] = Time.now
    @sessions[session_id] = data
  }
end