Apache-Session-Counted
view release on metacpan or search on metacpan
lib/Apache/Session/Counted.pm view on Meta::CPAN
warn "A:S:Counted: Counterfile problem, trying to repair...";
if (-e $cf) {
warn "A:S:Counted: Retrying after removing $cf.";
unlink $cf; # May fail. stupid enough that we are here.
$c = File::CounterFile->new($cf,"0");
} else {
require File::Basename;
my $dirname = File::Basename::dirname($cf);
my @mkdir;
while (! -d $dirname) {
push @mkdir, $dirname;
$dirname = File::Basename::dirname($dirname);
}
while (@mkdir) {
my $dirname = pop @mkdir;
mkdir $dirname, 0755 or die "Couldn't mkdir $dirname. Please create it with appropriate permissions";
}
$c = File::CounterFile->new($cf,"0");
}
warn "A:S:Counted: Counterfile problem successfully reapired.";
}
my $rhexid = sprintf "%08x", $c->inc;
my $hexid = scalar reverse $rhexid; # optimized for treestore. Not
# everything in one directory
# we have entropy as bad as rand(). Typically not very good.
my $password = sprintf "%08x%08x", rand(0xffffffff), rand(0xffffffff);
if (exists $self->{args}{HostID}) {
return sprintf "%s:%s_%s", $self->{args}{HostID}, $hexid, $password;
} else {
return $hexid . "_" . $password;
}
}
1;
=head1 NAME
Apache::Session::Counted - Session management via a File::CounterFile
=head1 SYNOPSIS
tie %s, 'Apache::Session::Counted', $sessionid, {
Directory => <root of directory tree>,
DirLevels => <number of dirlevels>,
CounterFile => <filename for File::CounterFile>,
AlwaysSave => <boolean>,
HostID => <string>,
HostURL => <callback>,
Timeout => <seconds>,
}
=head1 DESCRIPTION
This session module is based on Apache::Session, but it persues a
different notion of a session, so you probably have to adjust your
expectations a little.
The dialog that is implemented within an HTTP based application is a
nonlinear chain of events. The user can decide to use the back button
at any time without informing the application about it. A proper
session management must be prepared for this and must maintain the
state of every single event. For handling the notion of a session and
the notion of a registered user, the application has to differentiate
carefully between global state of user data and a user's session
related state. Some data may expire after a day, others may be
regarded as unexpirable. This module is solely responsible for
handling session related data. Saving unexpirable user related data
must be handled by the calling application.
In Apache::Session::Counted, a session-ID only lasts from one request
to the next at which point a new session-ID is computed by the
File::CounterFile module. Thus what you have to treat differently than
in Apache::Session are those parts that rely on the session-ID as a
fixed token per user. Accordingly, there is no option to delete a
session. The remove method is simply disabled as old session data will
be overwritten as soon as the counter is reset to zero.
The usage of the module is via a tie as described in the synopsis. The
arguments have the following meaning:
=over
=item Directory, DirLevels
Works similar to filestore but as most file systems are slow on large
directories, works in a tree of subdirectories.
=item CounterFile
A filename to be used by the File::CounterFile module. By changing
that file or the filename periodically, you can achieve arbitrary
patterns of key generation. If you do not specify a CounterFile, you
promise that in this session there is no need to generate a new ID and
that the whole purpose of this object is to retrieve previously stored
session data. Thus no new session file will be written. If you break
your promise and write something to the session hash, the retrieved
session file will be overwritten.
=item AlwaysSave
A boolean which, if true, forces storing of session data in any case.
If false, only a STORE, DELETE or CLEAR trigger that the session file
will be written when the tied hash goes out of scope. This has the
advantage that you can retrieve an old session without storing its
state again.
=item HostID
A string that serves as an identifier for the host we are running on.
This string will become part of the session-ID and must not contain a
colon. This can be used in a cluster environment so that a load
balancer or other interested parties can retrieve the session data
again.
=item HostURL
A callback that returns the service URL that can be called to get at
the session data from another host. This is needed in a cluster
environment. Two arguments are passed to this callback: HostID and
( run in 2.261 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )