Class: Raw::Session
- Inherits:
-
Hash
- Object
- Hash
- Raw::Session
- Includes:
- Expirable
- Defined in:
- lib/raw/context/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.
155 156 157 158 159 |
# File 'lib/raw/context/session.rb', line 155 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).
78 79 80 |
# File 'lib/raw/context/session.rb', line 78 def cache @cache end |
Instance Attribute Details
#session_id ⇒ Object (readonly)
The unique id of this session.
148 149 150 |
# File 'lib/raw/context/session.rb', line 148 def session_id @session_id end |
Class Method Details
.count ⇒ Object
The number of active (online) sessions. DON’T use yet!
119 120 121 |
# File 'lib/raw/context/session.rb', line 119 def count Session.cache.size end |
.current ⇒ Object
Returns the current session from the context thread local variable.
140 141 142 |
# File 'lib/raw/context/session.rb', line 140 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.
126 127 128 129 130 131 132 133 134 |
# File 'lib/raw/context/session.rb', line 126 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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/raw/context/session.rb', line 94 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.
84 85 86 87 |
# File 'lib/raw/context/session.rb', line 84 def setup(type = Session.cache_type) # gmosx: RDoc friendly. require "raw/context/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.
165 166 167 |
# File 'lib/raw/context/session.rb', line 165 def sync Session.cache[@session_id] = self end |
#touch! ⇒ Object
170 171 172 |
# File 'lib/raw/context/session.rb', line 170 def touch! expires_after(Session.keepalive) end |