Skip to content

Commit

Permalink
Factor out unique id generator. Expose cgi to session store.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6174 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Feb 19, 2007
1 parent 9e4c8b8 commit 772e02d
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions actionpack/lib/action_controller/cgi_ext/session_performance_fix.rb
Expand Up @@ -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

0 comments on commit 772e02d

Please sign in to comment.