On 4/27/06, Igor Chudov wrote:
> I run algebra.com with a few thousand registered users and a lot more
> unregistered. I use Apache::Session to store their session info. Right
> now I clean sessions every month. I would like to be smarter and clean
> sessions based on date information (ie remove sessions that are more
> than 3 weeks old).
>
> Can I have a date as a relational column in the sessions table?
>
> I know that I could maintain a date in the session data blob, but, it
> is expensive to use that for a few reasons.


You can have the database do all that for you using a trigger (if your
database suports it). I have used PostgreSQL in the past to do the
following:

CREATE TABLE sessions (
id varchar(32) NOT NULL PRIMARY KEY,
a_session text NOT NULL,
lm timestamp with time zone DEFAULT now()
);
CREATE FUNCTION update_session_lm() RETURNS "trigger"
AS '
BEGIN
NEW.lm :=3D ''now'';
RETURN NEW;
END;
'
LANGUAGE plpgsql;

CREATE TRIGGER update_session_lm_trig
BEFORE UPDATE ON sessions
FOR EACH ROW
EXECUTE PROCEDURE update_session_lm();

The deleting the sessions becomes a simple SQL statement against the
lm column. And this requires no code changes as everything is handled
in the database.

There is always a trade off though. This means more load up front
when the sessions are created and/or altered. The database has to do
slightly more work on each request to make the expiring of sessions
easy for you in the end.

Cheers,

Cees