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