Class: Merb::MemCacheSession

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#dataObject

Returns the value of attribute data.



48
49
50
# File 'lib/merb-core/dispatch/session/memcached.rb', line 48

def data
  @data
end

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
  @needs_new_cookie
end

#session_idObject

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

.generateObject

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.

Returns:

  • (Boolean)


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

#deleteObject

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.

Returns:

  • (Boolean)


124
125
126
# File 'lib/merb-core/dispatch/session/memcached.rb', line 124

def loaded?
  !! @data
end

#refresh_expirationObject

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.needs_new_cookie=true 
end

#regenerateObject

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.needs_new_cookie=true 
end