Class: Merb::MemorySessionContainer

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

Overview

Used for handling multiple sessions stored in memory.

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object

Parameters

key<String>

ID of the session to retrieve.

Returns

MemorySession

The session corresponding to the ID.



177
178
179
180
181
182
# File 'lib/merb-core/dispatch/session/memory.rb', line 177

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

.[]=(key, val) ⇒ Object

Parameters

key<String>

ID of the session to set.

val<MemorySession>

The session to set.



187
188
189
190
191
192
# File 'lib/merb-core/dispatch/session/memory.rb', line 187

def []=(key, val) 
  @mutex.synchronize {
    @timestamps[key] = Time.now
    @sessions[key] = val
  } 
end

.create(opts = {}) ⇒ Object

Creates a new session based on the options.

Parameters

opts<Hash>

The session options (see below).

Options (opts)

:session_id<String>

ID of the session to create in the container.

:data<MemorySession>

The session to create in the container.



168
169
170
# File 'lib/merb-core/dispatch/session/memory.rb', line 168

def create(opts={})
  self[opts[:session_id]] = opts[:data]
end

.delete(key) ⇒ Object

Parameters

key<String>

ID of the session to delete.



196
197
198
199
200
201
# File 'lib/merb-core/dispatch/session/memory.rb', line 196

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

.reap_old_sessionsObject

Deletes any sessions that have reached their maximum validity.



204
205
206
207
208
209
210
211
# File 'lib/merb-core/dispatch/session/memory.rb', line 204

def reap_old_sessions
  @timestamps.each do |key,stamp|
    if stamp + @session_ttl < Time.now
      delete(key)
    end  
  end
  GC.start
end

.sessionsObject

Returns

Array

The sessions stored in this container.



225
226
227
# File 'lib/merb-core/dispatch/session/memory.rb', line 225

def sessions
  @sessions
end

.setup(ttl = nil) ⇒ Object

Parameters

ttl<Fixnum>

Session validity time in seconds. Defaults to 1 hour.

Returns

MemorySessionContainer

The new session container.



151
152
153
154
155
156
157
158
# File 'lib/merb-core/dispatch/session/memory.rb', line 151

def setup(ttl=nil)
  @sessions = Hash.new
  @timestamps = Hash.new
  @mutex = Mutex.new
  @session_ttl = ttl || 60*60 # default 1 hour
  start_timer
  self
end

.start_timerObject

Starts the timer that will eventually reap outdated sessions.



214
215
216
217
218
219
220
221
# File 'lib/merb-core/dispatch/session/memory.rb', line 214

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