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'
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.
53 54 55 56 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 53 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.
155 156 157 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 155 def method_missing(name, *args, &block) @data.send(name, *args, &block) end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
48 49 50 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 48 def data @data end |
#needs_new_cookie ⇒ Object
Returns the value of attribute needs_new_cookie.
49 50 51 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 49 def @needs_new_cookie end |
#session_id ⇒ Object
Returns the value of attribute session_id.
47 48 49 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 47 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.
64 65 66 67 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 64 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.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 76 def persist(session_id) unless session_id.blank? session = ::Cache.get("session:#{session_id}") 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.
99 100 101 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 99 def reloadable? #:nodoc: 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.
140 141 142 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 140 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.
131 132 133 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 131 def []=(k, v) @data[k] = v end |
#delete ⇒ Object
Deletes the session by emptying stored data.
118 119 120 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 118 def delete @data = {} end |
#each(&b) ⇒ Object
Yields the session data to an each block.
Parameter
- &b
-
The block to pass to each.
148 149 150 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 148 def each(&b) @data.each(&b) end |
#loaded? ⇒ Boolean
Returns
- Boolean
-
True if session has been loaded already.
124 125 126 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 124 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.
113 114 115 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 113 def refresh_expiration self.=true end |
#regenerate ⇒ Object
Regenerate the session ID.
106 107 108 109 |
# File 'lib/merb-core/dispatch/session/memcached.rb', line 106 def regenerate @session_id = Merb::SessionMixin::rand_uuid self.=true end |