class Merb::SessionStoreContainer
Constants
- GENERATE_MAX_TRIES
Determines how many times to try generating a unique session key before we give up
Attributes
:api: private
Public Class Methods
Generates a new session ID and creates a new session.
Returns¶ ↑
- SessionStoreContainer
-
The new session.
:api: private
# File lib/merb-core/dispatch/session/store_container.rb, line 59 def generate # make sure we generate a unique session uuid sid = nil GENERATE_MAX_TRIES.times do |i| sid = Merb::SessionMixin.rand_uuid data = store.retrieve_session(sid) rescue nil break if data.nil? raise "Unable to Generate Unique Session key" if i == (GENERATE_MAX_TRIES-1) end session = new(sid) session.needs_new_cookie = true session end
Setup a new session or retreive an existing session.
Parameters¶ ↑
- request<Merb::Request>
-
The Merb::Request that came in from Rack.
Notes¶ ↑
If no sessions were found, a new SessionContainer will be generated.
Returns¶ ↑
:api: private
# File lib/merb-core/dispatch/session/store_container.rb, line 87 def setup(request) session = retrieve(request.session_id) request.session = session # TODO Marshal.dump is slow - needs optimization session._fingerprint = Marshal.dump(request.session.to_hash).hash session end
Private Class Methods
Parameters¶ ↑
- session_id<String
-
The ID of the session to retrieve.
Returns¶ ↑
- SessionStoreContainer
-
SessionStoreContainer instance with the session data. If no
sessions matched session_id, a new SessionStoreContainer will be generated.
Notes¶ ↑
If there are persisted exceptions callbacks to execute, they all get executed when Memcache library raises an exception.
:api: private
# File lib/merb-core/dispatch/session/store_container.rb, line 109 def retrieve(session_id) unless session_id.blank? begin session_data = store.retrieve_session(session_id) rescue => err Merb.logger.warn!("Could not retrieve session from #{self.name}: #{err.message}") end # Not in container, but assume that cookie exists session_data = new(session_id) if session_data.nil? else # No cookie...make a new session_id session_data = generate end if session_data.is_a?(self) session_data else # Recreate using the existing session as the data, when switching # from another session type for example, eg. cookie to memcached # or when the data is just a hash new(session_id).update(session_data) end end
Public Instance Methods
Teardown and/or persist the current session.
If @_destroy is true, clear out the session completely, including removal of the session cookie itself.
Parameters¶ ↑
- request<Merb::Request>
-
The Merb::Request that came in from Rack.
Notes¶ ↑
The data (self) is converted to a Hash first, since a container might choose to do a full Marshal on the data, which would make it persist attributes like 'needs_new_cookie', which it shouldn't.
:api: private
# File lib/merb-core/dispatch/session/store_container.rb, line 148 def finalize(request) if @_destroy store.delete_session(self.session_id) request.destroy_session_cookie else if _fingerprint != Marshal.dump(data = self.to_hash).hash begin store.store_session(request.session(self.class.session_store_type).session_id, data) rescue => err Merb.logger.warn!("Could not persist session to #{self.class.name}: #{err.message}") end end if needs_new_cookie || Merb::SessionMixin.needs_new_cookie? request.set_session_id_cookie(self.session_id) end end end
Regenerate the session ID.
:api: private
# File lib/merb-core/dispatch/session/store_container.rb, line 169 def regenerate store.delete_session(self.session_id) self.session_id = Merb::SessionMixin.rand_uuid store.store_session(self.session_id, self) end