Class: Merb::MemorySessionContainer
- Defined in:
- lib/merb-core/dispatch/session/memory.rb
Overview
Used for handling multiple sessions stored in memory.
Class Method Summary collapse
-
.[](key) ⇒ Object
Parameters key<String>:: ID of the session to retrieve.
-
.[]=(key, val) ⇒ Object
Parameters key<String>:: ID of the session to set.
-
.create(opts = {}) ⇒ Object
Creates a new session based on the options.
-
.delete(key) ⇒ Object
Parameters key<String>:: ID of the session to delete.
-
.reap_old_sessions ⇒ Object
Deletes any sessions that have reached their maximum validity.
-
.sessions ⇒ Object
Returns Array:: The sessions stored in this container.
-
.setup(ttl = nil) ⇒ Object
Parameters ttl<Fixnum>:: Session validity time in seconds.
-
.start_timer ⇒ Object
Starts the timer that will eventually reap outdated sessions.
Class Method Details
.[](key) ⇒ Object
Parameters
- key<String>
-
ID of the session to retrieve.
Returns
- MemorySession
-
The session corresponding to the ID.
184 185 186 187 188 189 |
# File 'lib/merb-core/dispatch/session/memory.rb', line 184 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.
194 195 196 197 198 199 |
# File 'lib/merb-core/dispatch/session/memory.rb', line 194 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.
175 176 177 |
# File 'lib/merb-core/dispatch/session/memory.rb', line 175 def create(opts={}) self[opts[:session_id]] = opts[:data] end |
.delete(key) ⇒ Object
Parameters
- key<String>
-
ID of the session to delete.
203 204 205 206 207 208 |
# File 'lib/merb-core/dispatch/session/memory.rb', line 203 def delete(key) @mutex.synchronize { @sessions.delete(key) @timestamps.delete(key) } end |
.reap_old_sessions ⇒ Object
Deletes any sessions that have reached their maximum validity.
211 212 213 214 215 216 217 218 |
# File 'lib/merb-core/dispatch/session/memory.rb', line 211 def reap_old_sessions @timestamps.each do |key,stamp| if stamp + @session_ttl < Time.now delete(key) end end GC.start end |
.sessions ⇒ Object
Returns
- Array
-
The sessions stored in this container.
232 233 234 |
# File 'lib/merb-core/dispatch/session/memory.rb', line 232 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.
158 159 160 161 162 163 164 165 |
# File 'lib/merb-core/dispatch/session/memory.rb', line 158 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_timer ⇒ Object
Starts the timer that will eventually reap outdated sessions.
221 222 223 224 225 226 227 228 |
# File 'lib/merb-core/dispatch/session/memory.rb', line 221 def start_timer Thread.new do loop { sleep @session_ttl reap_old_sessions } end end |