| 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 |
|---|
| | 26 | # This constructor is used internally by CGI::Session. The |
|---|
| | 27 | # user does not generally need to call it directly. |
|---|
| | 28 | # |
|---|
| | 29 | # +session+ is the session for which this instance is being |
|---|
| | 30 | # created. The session id must only contain alphanumeric |
|---|
| | 31 | # characters; automatically generated session ids observe |
|---|
| | 32 | # this requirement. |
|---|
| | 33 | # |
|---|
| | 34 | # +options+ is a hash of options for the initializer. The |
|---|
| | 35 | # following options are recognized: |
|---|
| | 36 | # |
|---|
| | 37 | # cache:: an instance of a MemCache client to use as the |
|---|
| | 38 | # session cache. |
|---|
| | 39 | # |
|---|
| | 40 | # expires:: an expiry time value to use for session entries in |
|---|
| | 41 | # the session cache. +expires+ is interpreted in seconds |
|---|
| | 42 | # relative to the current time if itÂs less than 60*60*24*30 |
|---|
| | 43 | # (30 days), or as an absolute Unix time (e.g., Time#to_i) if |
|---|
| | 44 | # greater. If +expires+ is +0+, or not passed on +options+, |
|---|
| | 45 | # the entry will never expire. |
|---|
| | 46 | # |
|---|
| | 47 | # This session's memcache entry will be created if it does |
|---|
| | 48 | # not exist, or retrieved if it does. |
|---|
| | 49 | def initialize(session, options = {}) |
|---|
| | 50 | id = session.session_id |
|---|
| | 51 | unless check_id(id) |
|---|
| | 52 | raise ArgumentError, "session_id '%s' is invalid" % id |
|---|
| 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 its 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 |
|---|
| | 54 | @cache = options['cache'] |
|---|
| | 55 | @expires = options['expires'] || 0 |
|---|
| | 56 | @session_key = "session:#{id}" |
|---|
| | 57 | @session_data = {} |
|---|
| | 58 | # Add this key to the store if haven't done so yet |
|---|
| | 59 | unless @cache.get(@session_key) |
|---|
| | 60 | @cache.add(@session_key, @session_data, @expires) |
|---|