writing CGI::Session sessions only when necessary
Jonathan Swartz <[email protected]>
| Newsgroups | gmane.comp.lang.perl.modules.cgi-session.user,gmane.comp.apache.mod-perl |
|---|---|
| Message-ID | <[email protected]> |
On our site we create a new CGI::Session object at the beginning of
the request, so that it can be used anywhere in the web code.
However, sessions are rarely written to, so at the end of the request
I'd like to avoid actually writing out a new session to backing store
unless a param actually got set. The expense of writing out new
sessions, and cleaning them up later, has become significant.
I couldn't figure out a way to do this with the public CGI::Session
API, but I managed to get it to work this way:
1 my $session = CGI::Session->new(...);
2 $session->_unset_status(CGI::Session::STATUS_MODIFIED);
3
4 # ... web code gets executed here ...
5
6 if (!$session->_test_status(CGI::Session::STATUS_MODIFIED)) {
7 $session->_reset_status;
8 }
9 $session->flush();
I initially reset the modified bit on line 2. Then on line 6, if the
modified bit is still unset (meaning no param was ever set), I clear
the status entirely, which causes the flush() on line 9 to be a no-op.
Is there a better, e.g. supported, way to do this? How do other people
prevent new sessions from getting written to the backing store
unnecessarily?
Thanks
Jon
------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com