aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/session
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-01-04 02:09:38 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-01-04 02:09:38 +0000
commit3e74ea89bce85a1e004841421ce6b06a2e47a52f (patch)
tree55f53697b45d32af9eaff4e2570e9a2832d136ee /actionpack/lib/action_controller/session
parentd05fa50914f00f204e61eef25a37a761d9a29fa5 (diff)
downloadrails-3e74ea89bce85a1e004841421ce6b06a2e47a52f.tar.gz
rails-3e74ea89bce85a1e004841421ce6b06a2e47a52f.tar.bz2
rails-3e74ea89bce85a1e004841421ce6b06a2e47a52f.zip
Added MemCacheStore for storing session data in Danga's MemCache system [Bob Cottrell]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@330 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_controller/session')
-rw-r--r--actionpack/lib/action_controller/session/mem_cache_store.rb95
1 files changed, 95 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/session/mem_cache_store.rb b/actionpack/lib/action_controller/session/mem_cache_store.rb
new file mode 100644
index 0000000000..5dc8f3bfef
--- /dev/null
+++ b/actionpack/lib/action_controller/session/mem_cache_store.rb
@@ -0,0 +1,95 @@
+# cgi/session/memcached.rb - persistent storage of marshalled session data
+#
+# == Overview
+#
+# This file provides the CGI::Session::MemCache class, which builds
+# persistence of storage data on top of the MemCache library. See
+# cgi/session.rb for more details on session storage managers.
+#
+
+begin
+ require 'cgi/session'
+ require 'memcache'
+
+ class CGI
+ class Session
+ # MemCache-based session storage class.
+ #
+ # This builds upon the top-level MemCache class provided by the
+ # library file memcache.rb. Session data is marshalled and stored
+ # in a memcached cache.
+ class MemCacheStore
+ def check_id(id) #:nodoc:#
+ /[^0-9a-zA-Z]+/ =~ id.to_s ? false : true
+ end
+
+ # Create a new CGI::Session::MemCache instance
+ #
+ # 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
+ # characters; automatically generated session ids observe
+ # this requirement.
+ #
+ # +option+ is a hash of options for the initialiser. The
+ # following options are recognized:
+ #
+ # cache:: an instance of a MemCache client to use as the
+ # session cache.
+ #
+ # 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
+ @cache = options['cache']
+ @session_key = "session:#{id}"
+ @hash = {}
+ end
+
+ # Restore session state from the session's memcache entry.
+ #
+ # Returns the session state as a hash.
+ def restore
+ begin
+ @hash = @cache[@session_key]
+ rescue
+ # Ignore session get failures.
+ end
+ @hash = {} unless @hash
+ @hash
+ end
+
+ # Save session state to the session's memcache entry.
+ def update
+ begin
+ @cache[@session_key] = @hash
+ rescue
+ # Ignore session update failures.
+ end
+ end
+
+ # Update and close the session's memcache entry.
+ def close
+ update
+ end
+
+ # Delete the session's memcache entry.
+ def delete
+ begin
+ @cache.delete(@session_key)
+ rescue
+ # Ignore session delete failures.
+ end
+ @hash = {}
+ end
+ end
+ end
+ end
+rescue LoadError
+ # MemCache wasn't available so neither can the store be
+end \ No newline at end of file