Like my previous question on object caching, this one is potentially a matter 
of style as well. When it comes to implementing expirations on session data, 
I've encountered two schools of thought on when is best to refresh the 
timestamp/expiration. In that the general idea of expiration is to discard 
information that hasn't been accessed in a while, some feel that updating the 
timestamp is best done during both loading and storing. After all, both are 
considered accessing the data. However, taking into account the general 
pattern of HTTP request processing, I feel that updating only during storage 
is best, especially when using a database for persistence.

Suppose one has a SQL table for saving session data. When a request comes in, 
the session is loaded and its expiration is examined. Assuming the session is 
still valid, one could issue another statement to the database to refresh the 
session's expiration time. That's two database ops before the session is even 
used. If you count the one at the end for storing the session back in the 
database it's a total of three per request. My feeling is that if you're 
going to be writing the session back within (hopefully) a fraction of a 
second anyway, you might as well wait until then to refresh the time-out.

The project I'm working on requires that I design a custom application 
platform for current and future projects. My proposed solution to the session 
management problem is as follows:

1) A fix-up handler is called to extract the session ID from a cookie. 
Assuming a valid ID was found, the session is loaded, de-serialized and 
checked for expiration. If all is well, the a reference to the session is 
stored in pnotes for use by the application.

1a) If for some reason no session was found (e.g. no cookie) a new one is 
created and a new cookie is stuffed in the outgoing headers.

2) During content-generation, the application obtains the session reference 
from pnotes and uses it as necessary.

3) A clean-up handler is called to re-serialize the session and stick it back 
in persistent storage (updating the expiration in the process). The handler 
of course does nothing if the application destroyed the session in step 2.

I'm still fairly new to mod_perl and haven't fully taken apart all of the 
various application servers out there to see how they do it. I would still 
appreciate any feedback anyone may have on the above.

Thanks in advance.

-- 
Milo Hyson
CyberLife Labs, LLC

Reply via email to