Class: CGI::Session::CachetasticStore

Inherits:
Object
  • Object
show all
Defined in:
lib/gems/cachetastic-2.1.2/lib/cachetastic/rails_extensions/cgi_session_store.rb

Overview

Allows Rails to use Cachetastic for it’s session store. The setting below needs to happen AFTER the gem has been required, obviously! In Rails 1.2.6 this is AFTER the initializer block.

ActionController::Base.session_store = :cachetastic_store

Instance Method Summary collapse

Constructor Details

#initialize(session, options = {}) ⇒ CachetasticStore

:nodoc:#



17
18
19
20
21
22
23
24
25
26
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/rails_extensions/cgi_session_store.rb', line 17

def initialize(session, options = {}) #:nodoc:#
  id = session.session_id
  unless check_id(id)
    raise ArgumentError, "session_id '%s' is invalid" % id
  end
  
  @session_key = id
  
  @session_data = {}
end

Instance Method Details

#check_id(id) ⇒ Object

:nodoc:#



13
14
15
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/rails_extensions/cgi_session_store.rb', line 13

def check_id(id) #:nodoc:#
  /[^0-9a-zA-Z]+/ =~ id.to_s ? false : true
end

#closeObject

Update and close the session’s memcache entry.



43
44
45
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/rails_extensions/cgi_session_store.rb', line 43

def close #:nodoc:#
  update
end

#dataObject

:nodoc:#



53
54
55
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/rails_extensions/cgi_session_store.rb', line 53

def data #:nodoc:#
  @session_data
end

#deleteObject

Delete the session’s memcache entry.



48
49
50
51
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/rails_extensions/cgi_session_store.rb', line 48

def delete #:nodoc:#
  Cachetastic::Caches::RailsSessionCache.delete(@session_key)
  @session_data = {}
end

#restoreObject

Restore session state from the session’s memcache entry.

Returns the session state as a hash.



31
32
33
34
35
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/rails_extensions/cgi_session_store.rb', line 31

def restore #:nodoc:#
  @session_data = Cachetastic::Caches::RailsSessionCache.get(@session_key) do
    {}
  end
end

#updateObject

Save session state to the session’s memcache entry.



38
39
40
# File 'lib/gems/cachetastic-2.1.2/lib/cachetastic/rails_extensions/cgi_session_store.rb', line 38

def update #:nodoc:#
  Cachetastic::Caches::RailsSessionCache.set(@session_key, @session_data)
end