Database Reference
In-Depth Information
Session expiration
The Apache::Session module requires only the id and a_session columns in the
perl_session table. The module makes no provision for timing out or expiring ses‐
sions, but doesn't restrict you from adding other columns, so you can implement those
capabilities yourself. Add a TIMESTAMP column to the table to store the time of each
session's last modification (MySQL updates it automatically whenever a session record
is changed):
ALTER TABLE perl_session
ADD update_time TIMESTAMP NOT NULL ,
ADD INDEX ( update_time );
To expire sessions, run a statement periodically that sweeps the table and removes old
rows. The following statement uses an expiration time of four hours:
DELETE FROM perl_session WHERE update_time < NOW () - INTERVAL 4 HOUR ;
The ALTER TABLE statement indexes update_time to make the DELETE operation faster.
To expire rows automatically, create a scheduled event (see Recipe 9.8 ). This event runs
every four hours:
CREATE EVENT expire_perl_session
ON SCHEDULE EVERY 4 HOUR
DO DELETE FROM perl_session WHERE update_time < NOW () - INTERVAL 4 HOUR ;
21.2. Using MySQL-Based Storage in Ruby Applications
Problem
You want to use session storage for Ruby scripts.
Solution
Use the CGI::Session class interface. By default, it uses temporary files for backing
store, but you can configure it to use MySQL instead.
Discussion
The CGI::Session class manages session storage. It identifies sessions using cookies,
which it adds transparently to the responses sent to the client. CGI::Session permits
use of alternative storage-management classes in place of the default manager that uses
temporary files. We'll use the mysql-session package, which is based on the Ruby DBI
interface and stores session records using MySQL. mysql-session is available from the
MySQL Cookbook companion website (see the Preface ).
To use mysql-session in a script, include these modules:
Search WWH ::




Custom Search