[Cgi-session-user] performance enhancement request
Brought to you by:
sherzodr
From: Rhesa R. <pe...@rh...> - 2007-05-14 13:21:47
|
Hi, Gearing up for a big upcoming event, where we expect many millions of new visitors in the space of a few hours, I've been working on making our web app as lean as possible. It quickly became obvious that CGI::Session was choking our database, and the reason for that is that it insists on flushing the session on every single request (to store the new ATIME). That makes any attempt at caching futile, so I've patched my local install to only accept changes to the ATIME once every minute or so. As a result, the load on the master database has dropped dramatically. It now also makes sense to use caching (in the form of CGI::Session::Driver::memcached_mysql, in my case), so reads are spread over many machines. I'd like to see code for this included in the main distribution though, as hacks like this have a tendency to break with upgrades. What I think is needed for this, is to decouple the ATIME code at the end of load(), and put that in its own method for easy overriding. Right now, load() is rather monolithic. Attached is my hack, in case it helps anyone. Rhesa =pod =head1 NAME CGI::Session::LessAtime - Stops flush() on every request =head1 USAGE use CGI::Session; use CGI::Session::LessAtime; # normal session code follows =head1 LIMITATIONS Only works with the default serializer. =cut package CGI::Session::LessAtime; use strict; my $interval = 60; #seconds use CGI::Session; use CGI::Session::Serialize::default; { no strict 'refs'; my $thaw_orig = \&CGI::Session::Serialize::default::thaw; *CGI::Session::Serialize::default::thaw = sub { my $ret = $thaw_orig->(@_); $ret->{__SESSION_ORIG_ATIME} = $ret->{_SESSION_ATIME}; return $ret; }; my $load_orig = \&CGI::Session::load; *CGI::Session::load = sub { my $ret = $load_orig->(@_); if( defined $ret and _elapsed( $ret ) < $interval ) { $ret->_unset_status( CGI::Session::STATUS_MODIFIED ); } return $ret; }; sub _elapsed { my $ret = shift; return $ret->{_DATA}{_SESSION_ATIME} - $ret->{_DATA}{__SESSION_ORIG_ATIME} ; } } 1; |