Apache-Session
view release on metacpan or search on metacpan
lib/Apache/Session.pm view on Meta::CPAN
=head1 INTERFACE
The interface to Apache::Session is very simple: tie a hash to the
desired class and use the hash as normal. The constructor takes two
optional arguments. The first argument is the desired session ID
number, or undef for a new session. The second argument is a hash
of options that will be passed to the object store and locker classes.
=head2 tieing the session
Get a new session using DBI:
tie %session, 'Apache::Session::MySQL', undef,
{ DataSource => 'dbi:mysql:sessions' };
Restore an old session from the database:
tie %session, 'Apache::Session::MySQL', $session_id,
{ DataSource => 'dbi:mysql:sessions' };
=head2 Storing and retrieving data to and from the session
Hey, how much easier could it get?
$session{first_name} = "Chuck";
$session{an_array_ref} = [ $one, $two, $three ];
$session{an_object} = Some::Class->new;
=head2 Reading the session ID
The session ID is the only magic entry in the session object,
but anything beginning with an "_" is considered reserved for
future use.
my $id = $session{_session_id};
=head2 Permanently removing the session from storage
tied(%session)->delete;
=head1 BEHAVIOR
Apache::Session tries to behave the way the author believes that
you would expect. When you create a new session, Session immediately
saves the session to the data store, or calls die() if it cannot. It
also obtains an exclusive lock on the session object. If you retrieve
an existing session, Session immediately restores the object from storage,
or calls die() in case of an error. Session also obtains a non-exclusive
lock on the session.
As you put data into the session hash, Session squirrels it away for
later use. When you untie() the session hash, or it passes out of
scope, Session checks to see if anything has changed. If so, Session
gains an exclusive lock and writes the session to the data store.
It then releases any locks it has acquired.
Note that Apache::Session does only a shallow check to see if anything has
changed. If nothing changes in the top level tied hash, the data will not be
updated in the backing store. You are encouraged to timestamp the session hash
so that it is sure to be updated.
When you call the delete() method on the session object, the
object is immediately removed from the object store, if possible.
When Session encounters an error, it calls die(). You will probably
want to wrap your session logic in an eval block to trap these errors.
=head1 LOCKING AND TRANSACTIONS
By default, most Apache::Session implementations only do locking to prevent
data corruption. The locking scheme does not provide transactional
consistency, such as you might get from a relational database. If you desire
transactional consistency, you must provide the Transaction argument with a
true value when you tie the session hash. For example:
tie %s, 'Apache::Session::File', $id {
Directory => '/tmp/sessions',
LockDirectory => '/var/lock/sessions',
Transaction => 1
};
Note that the Transaction argument has no practical effect on the MySQL and
Postgres implementations. The MySQL implementation only supports exclusive
locking, and the Postgres implementation uses the transaction features of that
database.
=head1 IMPLEMENTATION
The way you implement Apache::Session depends on what you are
trying to accomplish. Here are some hints on which classes to
use in what situations
=head1 STRATEGIES
Apache::Session is mainly designed to track user session between
http requests. However, it can also be used for any situation
where data persistence is desirable. For example, it could be
used to share global data between your httpd processes. The
following examples are short mod_perl programs which demonstrate
some session handling basics.
=head2 Sharing data between Apache processes
When you share data between Apache processes, you need to decide on a
session ID number ahead of time and make sure that an object with that
ID number is in your object store before starting your Apache. How you
accomplish that is your own business. I use the session ID "1". Here
is a short program in which we use Apache::Session to store out
database access information.
use Apache;
use Apache::Session::File;
use DBI;
use strict;
my %global_data;
eval {
tie %global_data, 'Apache::Session::File', 1,
lib/Apache/Session.pm view on Meta::CPAN
Catalyst::Plugin::Session - support of sessions in Catalyst
Session - OO interface to Apache::Session
=head1 LICENSE
Under the same terms as Perl itself.
=head1 AUTHORS
Alexandr Ciornii, L<http://chorny.net> - current maintainer
Jeffrey Baker <jwbaker@acm.org> is the author of
Apache::Session.
Tatsuhiko Miyagawa <miyagawa@bulknews.net> is the author of
Generate::ModUniqueID and Generate::ModUsertrack
Erik Rantapaa <rantapaa@fanbuzz.com> found errors in both Lock::File
and Store::File
Bart Schaefer <schaefer@zanshin.com> notified me of a bug in
Lock::File.
Chris Winters <cwinters@intes.net> contributed the Sybase code.
Michael Schout <mschout@gkg.net> fixed a commit policy bug in 1.51.
Andreas J. Koenig <andreas.koenig@anima.de> contributed valuable CPAN
advice and also Apache::Session::Tree and Apache::Session::Counted.
Gerald Richter <richter@ecos.de> had the idea for a tied hash interface
and provided the initial code for it. He also uses Apache::Session in
his Embperl module and is the author of Apache::Session::Embperl
Jochen Wiedmann <joe@ipsoft.de> contributed patches for bugs and
improved performance.
Steve Shreeve <shreeve@uci.edu> squashed a bug in 0.99.0 whereby
a cleared hash or deleted key failed to set the modified bit.
Peter Kaas <Peter.Kaas@lunatech.com> sent quite a bit of feedback
with ideas for interface improvements.
Randy Harmon <rjharmon@uptimecomputers.com> contributed the original
storage-independent object interface with input from:
Bavo De Ridder <bavo@ace.ulyssis.student.kuleuven.ac.be>
Jules Bean <jmlb2@hermes.cam.ac.uk>
Lincoln Stein <lstein@cshl.org>
Jamie LeTaul <jletual@kmtechnologies.com> fixed file locking on Windows.
Scott McWhirter <scott@surreytech.co.uk> contributed verbose error messages for
file locking.
Corris Randall <corris@line6.net> gave us the option to use any table name in
the MySQL store.
Oliver Maul <oliver.maul@ixos.de> updated the Sybase modules
Innumerable users sent a patch for the reversed file age test in the file
locking module.
Langen Mike <mike.langen@tamedia.ch> contributed Informix modules.
=cut
package Apache::Session;
use strict;
use vars qw($VERSION);
$VERSION = '1.94';
$VERSION = eval $VERSION;
#State constants
#
#These constants are used in a bitmask to store the
#object's status. New indicates that the object
#has not yet been inserted into the object store.
#Modified indicates that a member value has been
#changed. Deleted is set when delete() is called.
#Synced indicates that an object has been materialized
#from the datastore.
sub NEW () {1};
sub MODIFIED () {2};
sub DELETED () {4};
sub SYNCED () {8};
#State methods
#
#These methods aren't used anymore for performance reasons. I'll
#keep them around for reference
sub is_new { $_[0]->{status} & NEW }
sub is_modified { $_[0]->{status} & MODIFIED }
sub is_deleted { $_[0]->{status} & DELETED }
sub is_synced { $_[0]->{status} & SYNCED }
sub make_new { $_[0]->{status} |= NEW }
sub make_modified { $_[0]->{status} |= MODIFIED }
sub make_deleted { $_[0]->{status} |= DELETED }
sub make_synced { $_[0]->{status} |= SYNCED }
sub make_old { $_[0]->{status} &= ($_[0]->{status} ^ NEW) }
sub make_unmodified { $_[0]->{status} &= ($_[0]->{status} ^ MODIFIED) }
sub make_undeleted { $_[0]->{status} &= ($_[0]->{status} ^ DELETED) }
sub make_unsynced { $_[0]->{status} &= ($_[0]->{status} ^ SYNCED) }
#Tie methods
#
#Here we are hiding our complex data persistence framework behind
( run in 1.224 second using v1.01-cache-2.11-cpan-df04353d9ac )