Class: Merb::MemCacheSession
- Defined in:
- lib/merb-core/dispatch/session/memcached.rb
Overview
Sessions stored in memcached.
Requires setup in your init.rb
.
require 'memcache'
CACHE = MemCache.new('127.0.0.1:11211', { :namespace => 'my_app' })
And a setting in init.rb
:
c[:session_store] = 'memcache'
If you are using the memcached gem instead of memcache-client, you must setup like this:
require 'memcached'
CACHE = Memcached.new('127.0.0.1:11211', { :namespace => 'my_app' })
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#needs_new_cookie ⇒ Object
Returns the value of attribute needs_new_cookie.
-
#session_id ⇒ Object
Returns the value of attribute session_id.
Class Method Summary collapse
-
.generate ⇒ Object
Generates a new session ID and creates a new session.
-
.persist(session_id) ⇒ Object
Parameters session_id<String:: The ID of the session to retrieve.
-
.reloadable? ⇒ Boolean
Don’t try to reload in dev mode.
Instance Method Summary collapse
-
#[](k) ⇒ Object
Parameters k<~to_s>:: The key of the session parameter to retrieve.
-
#[]=(k, v) ⇒ Object
Parameters k<~to_s>:: The key of the session parameter to set.
-
#delete ⇒ Object
Deletes the session by emptying stored data.
-
#each(&b) ⇒ Object
Yields the session data to an each block.
-
#initialize(session_id) ⇒ MemCacheSession
constructor
Parameters session_id<String>:: A unique identifier for this session.
-
#loaded? ⇒ Boolean
Returns Boolean:: True if session has been loaded already.
-
#refresh_expiration ⇒ Object
Recreates the cookie with the default expiration time.
-
#regenerate ⇒ Object
Regenerate the session ID.
Constructor Details
#initialize(session_id) ⇒ MemCacheSession
Parameters
- session_id<String>
-
A unique identifier for this session.
68 69 70 71 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 68 def initialize(session_id) @session_id = session_id @data = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
Attempts to redirect any messages to the data object.
178 179 180 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 178 def method_missing(name, *args, &block) @data.send(name, *args, &block) end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
63 64 65 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 63 def data @data end |
#needs_new_cookie ⇒ Object
Returns the value of attribute needs_new_cookie.
64 65 66 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 64 def @needs_new_cookie end |
#session_id ⇒ Object
Returns the value of attribute session_id.
62 63 64 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 62 def session_id @session_id end |
Class Method Details
.generate ⇒ Object
Generates a new session ID and creates a new session.
Returns
- MemCacheSession
-
The new session.
79 80 81 82 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 79 def generate sid = Merb::SessionMixin::rand_uuid new(sid) end |
.persist(session_id) ⇒ Object
Parameters
- session_id<String
-
The ID of the session to retrieve.
Returns
- Array
-
A pair consisting of a MemCacheSession and the session’s ID. If no sessions matched session_id, a new MemCacheSession will be generated.
Notes
If there are persiste exceptions callbacks to execute, they all get executed when Memcache library raises an exception.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 95 def persist(session_id) unless session_id.blank? begin session = CACHE.get("session:#{session_id}") rescue => err Merb.logger.warn!("Could not persist session to MemCache: #{err.}") Merb::SessionMixin::persist_exception_callbacks.each {|x| x.call(err) } end if session.nil? # Not in memcached, but assume that cookie exists session = new(session_id) end else # No cookie...make a new session_id session = generate end if session.is_a?(MemCacheSession) [session, session.session_id] else # recreate using the rails session as the data session_object = MemCacheSession.new(session_id) session_object.data = session [session_object, session_object.session_id] end end |
.reloadable? ⇒ Boolean
Don’t try to reload in dev mode.
122 123 124 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 122 def reloadable? false end |
Instance Method Details
#[](k) ⇒ Object
Parameters
- k<~to_s>
-
The key of the session parameter to retrieve.
Returns
- String
-
The value of the session parameter.
163 164 165 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 163 def [](k) @data[k] end |
#[]=(k, v) ⇒ Object
Parameters
- k<~to_s>
-
The key of the session parameter to set.
- v<~to_s>
-
The value of the session parameter to set.
154 155 156 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 154 def []=(k, v) @data[k] = v end |
#delete ⇒ Object
Deletes the session by emptying stored data.
141 142 143 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 141 def delete @data = {} end |
#each(&b) ⇒ Object
Yields the session data to an each block.
Parameter
- &b
-
The block to pass to each.
171 172 173 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 171 def each(&b) @data.each(&b) end |
#loaded? ⇒ Boolean
Returns
- Boolean
-
True if session has been loaded already.
147 148 149 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 147 def loaded? !! @data end |
#refresh_expiration ⇒ Object
Recreates the cookie with the default expiration time. Useful during log in for pushing back the expiration date.
136 137 138 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 136 def refresh_expiration self.=true end |
#regenerate ⇒ Object
Regenerate the session ID.
129 130 131 132 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 129 def regenerate @session_id = Merb::SessionMixin::rand_uuid self.=true end |