From 772e02dd62989dbe08cc774a5ada2ae6430033ad Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 19 Feb 2007 23:51:25 +0000 Subject: [PATCH] Factor out unique id generator. Expose cgi to session store. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6174 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../cgi_ext/session_performance_fix.rb | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/actionpack/lib/action_controller/cgi_ext/session_performance_fix.rb b/actionpack/lib/action_controller/cgi_ext/session_performance_fix.rb index 7305167860551..6f9a09da6de59 100644 --- a/actionpack/lib/action_controller/cgi_ext/session_performance_fix.rb +++ b/actionpack/lib/action_controller/cgi_ext/session_performance_fix.rb @@ -3,28 +3,40 @@ # when serving requests from a long-lived process. # # http://railsexpress.de/blog/articles/2005/11/22/speeding-up-the-creation-of-new-sessions +# +# Also expose the CGI instance to session stores. require 'cgi/session' require 'digest/md5' class CGI class Session #:nodoc: + # Generate an MD5 hash including the time, a random number, the process id, + # and a constant string. This is used to generate session ids but may be + # reused elsewhere. + def self.generate_unique_id(constant = 'foobar') + md5 = Digest::MD5.new + now = Time.now + md5 << now.to_s + md5 << String(now.usec) + md5 << String(rand(0)) + md5 << String($$) + md5 << constant + md5.hexdigest + end + + # Make the CGI instance available to session stores. + attr_reader :cgi + alias_method :initialize_without_cgi_reader, :initialize + def initialize(cgi, options = {}) + @cgi = cgi + initialize_without_cgi_reader(cgi, options) + end + private # Create a new session id. - # - # The session id is an MD5 hash based upon the time, - # a random number, and a constant string. This routine - # is used internally for automatically generated - # session ids. def create_new_id - md5 = Digest::MD5::new - now = Time::now - md5.update(now.to_s) - md5.update(String(now.usec)) - md5.update(String(rand(0))) - md5.update(String($$)) - md5.update('foobar') @new_session = true - md5.hexdigest + self.class.generate_unique_id end end end