From 6236d518f29da02e26be8c5fbc73da723aff6daa Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 Jan 2006 22:21:26 +0000 Subject: Added the possibility to specify atomatic expiration for the memcachd session container (closes #3571) [Stefan Kaes] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3465 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../action_controller/session/mem_cache_store.rb | 30 +++++++++++++--------- 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/session/mem_cache_store.rb b/actionpack/lib/action_controller/session/mem_cache_store.rb index 6fc80d9e56..a7076fcd5d 100644 --- a/actionpack/lib/action_controller/session/mem_cache_store.rb +++ b/actionpack/lib/action_controller/session/mem_cache_store.rb @@ -25,30 +25,38 @@ begin # Create a new CGI::Session::MemCache instance # - # This constructor is used internally by CGI::Session. The + # This constructor is used internally by CGI::Session. The # user does not generally need to call it directly. # # +session+ is the session for which this instance is being - # created. The session id must only contain alphanumeric + # created. The session id must only contain alphanumeric # characters; automatically generated session ids observe # this requirement. # - # +option+ is a hash of options for the initializer. The + # +options+ is a hash of options for the initializer. The # following options are recognized: # # cache:: an instance of a MemCache client to use as the # session cache. # + # expires:: an expiry time value to use for session entries in + # the session cache. +expires+ is interpreted in seconds + # relative to the current time if it’s less than 60*60*24*30 + # (30 days), or as an absolute Unix time (e.g., Time#to_i) if + # greater. If +expires+ is +0+, or not passed on +options+, + # the entry will never expire. + # # This session's memcache entry will be created if it does # not exist, or retrieved if it does. def initialize(session, options = {}) id = session.session_id unless check_id(id) raise ArgumentError, "session_id '%s' is invalid" % id - end + end @cache = options['cache'] || MemCache.new('localhost') - @session_key = "session:#{id}" - @hash = {} + @expires = options['expires'] || 0 + @session_key = "session:#{id}" + @session_data = {} end # Restore session state from the session's memcache entry. @@ -56,18 +64,16 @@ begin # Returns the session state as a hash. def restore begin - @hash = @cache[@session_key] + @session_data = @cache[@session_key] || {} rescue - # Ignore session get failures. + @session_data = {} end - @hash = {} unless @hash - @hash end # Save session state to the session's memcache entry. def update begin - @cache[@session_key] = @hash + @cache.set(@session_key, @session_data, @expires) rescue # Ignore session update failures. end @@ -85,7 +91,7 @@ begin rescue # Ignore session delete failures. end - @hash = {} + @session_data = {} end end end -- cgit v1.2.3