Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/actionpack/lib/action_controller/session/mem_cache_store.rb

Revision 7885, 3.2 kB (checked in by bitsweat, 7 months ago)

Memcached sessions: add session data on initialization; don't silently discard exceptions; add unit tests. Closes #9823.

Line 
1 # cgi/session/memcached.rb - persistent storage of marshalled session data
2 #
3 # == Overview
4 #
5 # This file provides the CGI::Session::MemCache class, which builds
6 # persistence of storage data on top of the MemCache library.  See
7 # cgi/session.rb for more details on session storage managers.
8 #
9
10 begin
11   require 'cgi/session'
12   require_library_or_gem 'memcache'
13
14   class CGI
15     class Session
16       # MemCache-based session storage class.
17       #
18       # This builds upon the top-level MemCache class provided by the
19       # library file memcache.rb.  Session data is marshalled and stored
20       # in a memcached cache.
21       class MemCacheStore
22         def check_id(id) #:nodoc:#
23           /[^0-9a-zA-Z]+/ =~ id.to_s ? false : true
24         end
25
26         # Create a new CGI::Session::MemCache instance
27         #
28         # This constructor is used internally by CGI::Session. The
29         # user does not generally need to call it directly.
30         #
31         # +session+ is the session for which this instance is being
32         # created. The session id must only contain alphanumeric
33         # characters; automatically generated session ids observe
34         # this requirement.
35         #
36         # +options+ is a hash of options for the initializer. The
37         # following options are recognized:
38         #
39         # cache::  an instance of a MemCache client to use as the
40         #      session cache.
41         #
42         # expires:: an expiry time value to use for session entries in
43         #     the session cache. +expires+ is interpreted in seconds
44         #     relative to the current time if itÂ’s less than 60*60*24*30
45         #     (30 days), or as an absolute Unix time (e.g., Time#to_i) if
46         #     greater. If +expires+ is +0+, or not passed on +options+,
47         #     the entry will never expire.
48         #
49         # This session's memcache entry will be created if it does
50         # not exist, or retrieved if it does.
51         def initialize(session, options = {})
52           id = session.session_id
53           unless check_id(id)
54             raise ArgumentError, "session_id '%s' is invalid" % id
55           end
56           @cache = options['cache'] || MemCache.new('localhost')
57           @expires = options['expires'] || 0
58           @session_key = "session:#{id}"
59           @session_data = {}
60           # Add this key to the store if haven't done so yet
61           unless @cache.get(@session_key)
62             @cache.add(@session_key, @session_data, @expires)
63           end
64         end
65
66         # Restore session state from the session's memcache entry.
67         #
68         # Returns the session state as a hash.
69         def restore
70           @session_data = @cache[@session_key] || {}
71         end
72
73         # Save session state to the session's memcache entry.
74         def update
75           @cache.set(@session_key, @session_data, @expires)
76         end
77      
78         # Update and close the session's memcache entry.
79         def close
80           update
81         end
82
83         # Delete the session's memcache entry.
84         def delete
85           @cache.delete(@session_key)
86           @session_data = {}
87         end
88        
89         def data
90           @session_data
91         end
92
93       end
94     end
95   end
96 rescue LoadError
97   # MemCache wasn't available so neither can the store be
98 end
Note: See TracBrowser for help on using the browser.