Class: Nitro::Session
- Inherits:
-
Hash
- Object
- Hash
- Nitro::Session
- Includes:
- Expirable
- Defined in:
- lib/nitro/session.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
(also: store)
The sessions cache (store).
Instance Attribute Summary collapse
-
#session_id ⇒ Object
readonly
The unique id of this session.
Class Method Summary collapse
-
.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.
-
.setup(type = Session.cache_type) ⇒ Object
Load the correct Session specialization according to the cache type.
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.
157 158 159 160 161 |
# File 'lib/nitro/session.rb', line 157 def initialize(context = nil) @session_id = create_id expires_after(Session.keepalive) created if respond_to?(:created) end |
Class Attribute Details
.cache ⇒ Object Also known as: store
The sessions cache (store).
80 81 82 |
# File 'lib/nitro/session.rb', line 80 def cache @cache end |
Instance Attribute Details
#session_id ⇒ Object (readonly)
The unique id of this session.
150 151 152 |
# File 'lib/nitro/session.rb', line 150 def session_id @session_id end |
Class Method Details
.count ⇒ Object
The number of active (online) sessions. DON’T use yet!
121 122 123 |
# File 'lib/nitro/session.rb', line 121 def count Session.cache.size end |
.current ⇒ Object
Returns the current session from the context thread local variable.
142 143 144 |
# File 'lib/nitro/session.rb', line 142 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.
128 129 130 131 132 133 134 135 136 |
# File 'lib/nitro/session.rb', line 128 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.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/nitro/session.rb', line 96 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 |
.setup(type = Session.cache_type) ⇒ Object
Load the correct Session specialization according to the cache type.
86 87 88 89 |
# File 'lib/nitro/session.rb', line 86 def setup(type = Session.cache_type) # gmosx: RDoc friendly. require 'nitro/session/' + type.to_s 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.
167 168 169 |
# File 'lib/nitro/session.rb', line 167 def sync Session.cache[@session_id] = self end |
#touch! ⇒ Object
172 173 174 |
# File 'lib/nitro/session.rb', line 172 def touch! expires_after(Session.keepalive) end |