Class: Nitro::Session
- Inherits:
-
Hash
- Object
- Hash
- Nitro::Session
- Includes:
- Expirable
- Defined in:
- lib/nitro/session.rb,
lib/nitro/session/drb.rb
Overview
A web application session.
State is a neccessary evil but session variables should be avoided as much as possible. Session state is typically distributed to many servers so avoid storing complete objects in session variables, only store oids and small integer/strings.
The session should be persistable to survive server shutdowns.
The session can be considered as a Hash where key-value pairs are stored. Typically symbols are used as keys. By convention uppercase symbols are used for internal Nitro session variables (ie :FLASH, :USER, etc). User applications typically use lowercase symbols (ie :cart, :history, etc).
– TODO: rehash of the session cookie TODO: store -> cache, reimplement helpers. ++
Class Attribute Summary collapse
-
.cache ⇒ Object
The sessions cache (store).
Instance Attribute Summary collapse
-
#session_id ⇒ Object
readonly
The unique id of this session.
Class Method Summary collapse
-
.cache_type=(cache_type) ⇒ Object
(also: set_cache_type, store_type=, set_store_type)
Set the session cache.
-
.count ⇒ Object
The number of active (online) sessions.
-
.current ⇒ Object
Returns the current session from the context thread local variable.
-
.garbage_collect ⇒ Object
(also: gc!)
Perform Session garbage collection.
-
.lookup(context) ⇒ Object
Lookup the session in the cache by using the session cookie value as a key.
Instance Method Summary collapse
-
#initialize(context = nil) ⇒ Session
constructor
Create the session for the given context.
-
#sync ⇒ Object
(also: #restore)
Synchronize the session store, by restoring this session.
- #touch! ⇒ Object
Constructor Details
#initialize(context = nil) ⇒ Session
Create the session for the given context. If the hook method ‘created’ is defined it is called at the end. Typically used to initialize the session hash.
147 148 149 150 151 |
# File 'lib/nitro/session.rb', line 147 def initialize(context = nil) @session_id = create_id expires_after(Session.keepalive) created if respond_to?(:created) end |
Class Attribute Details
.cache ⇒ Object
The sessions cache (store).
62 63 64 |
# File 'lib/nitro/session.rb', line 62 def cache @cache end |
Instance Attribute Details
#session_id ⇒ Object (readonly)
The unique id of this session.
140 141 142 |
# File 'lib/nitro/session.rb', line 140 def session_id @session_id end |
Class Method Details
.cache_type=(cache_type) ⇒ Object Also known as: set_cache_type, store_type=, set_store_type
Set the session cache. The generalized caching system in Glue is used. The following options are available:
-
:memory [default]
-
:drb
-
:og
-
:file
-
:memcached
73 74 75 76 |
# File 'lib/nitro/session.rb', line 73 def cache_type=(cache_type) # gmosx: RDoc friendly. require 'nitro/session/' + cache_type.to_s end |
.count ⇒ Object
The number of active (online) sessions. DON’T use yet!
111 112 113 |
# File 'lib/nitro/session.rb', line 111 def count Session.cache.size end |
.current ⇒ Object
Returns the current session from the context thread local variable.
132 133 134 |
# File 'lib/nitro/session.rb', line 132 def current Context.current.session end |
.garbage_collect ⇒ Object Also known as: gc!
Perform Session garbage collection. You may call this method from a cron job.
118 119 120 121 122 123 124 125 126 |
# File 'lib/nitro/session.rb', line 118 def garbage_collect expired = [] for s in Session.cache.all expired << s.session_id if s.expired? end for sid in expired Session.cache.delete(sid) end end |
.lookup(context) ⇒ Object
Lookup the session in the cache by using the session cookie value as a key. If the session does not exist creates a new session, inserts it in the cache and appends a new session cookie in the response.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/nitro/session.rb', line 86 def lookup(context) if session_id = context.[Session.] session = Session.cache[session_id] end unless session # Create new session. session = Session.new(context) = Cookie.new(Session., session.session_id) if Session. .expires = Time.now + Session.keepalive end context.() Session.cache[session.session_id] = session else # Access ('touch') the existing session. session.touch! end return session end |
Instance Method Details
#sync ⇒ Object Also known as: restore
Synchronize the session store, by restoring this session. Especially useful in distributed and/or multiprocess setups.
157 158 159 |
# File 'lib/nitro/session.rb', line 157 def sync Session.cache[@session_id] = self end |
#touch! ⇒ Object
162 163 164 |
# File 'lib/nitro/session.rb', line 162 def touch! expires_after(Session.keepalive) end |