use URI::Find;
use File::Temp 'tempdir';
-our $VERSION = '0.06';
+our $VERSION = '0.13';
__PACKAGE__->mk_classdata('_session');
__PACKAGE__->mk_accessors('sessionid');
=head1 SYNOPSIS
use Catalyst 'Session::FastMmap';
+
+ MyApp->config->{session} = {
+ expires => 3600,
+ rewrite => 1,
+ storage => '/tmp/session'
+ };
$c->session->{foo} = 'bar';
print $c->sessionid;
=head1 DESCRIPTION
-Fast sessions.
+C<Catalyst::Plugin::Session::FastMmap> is a fast session plugin for
+Catalyst that uses an mmap'ed file to act as a shared memory
+interprocess cache. It is based on C<Cache::FastMMap>.
+
=head2 EXTENDED METHODS
-=head3 finalize
+=over 4
+
+=item finalize
=cut
sub finalize {
- my $c = shift;
- unless ($c->config->{no_url_rewrite}) {
+ my $c = shift;
+ if ( $c->config->{session}->{rewrite} ) {
my $redirect = $c->response->redirect;
$c->response->redirect( $c->uri($redirect) ) if $redirect;
}
if ( my $cookie = $c->request->cookies->{session} ) {
$set = 0 if $cookie->value eq $sid;
}
- $c->response->cookies->{session} = { value => $sid } if $set;
- unless ($c->config->{no_url_rewrite}) {
- my $finder = URI::Find->new(
- sub {
- my ( $uri, $orig ) = @_;
- my $base = $c->request->base;
- return $orig unless $orig =~ /^$base/;
- return $orig if $uri->path =~ /\/-\//;
- return $c->uri($orig);
- }
- );
- $finder->find( \$c->res->{output} ) if $c->res->output;
+ if ( $set ) {
+ $c->response->cookies->{session} = {
+ value => $sid
+ };
+ }
+ if ( $c->config->{session}->{rewrite} ) {
+ my $finder = URI::Find->new(
+ sub {
+ my ( $uri, $orig ) = @_;
+ my $base = $c->request->base;
+ return $orig unless $orig =~ /^$base/;
+ return $orig if $uri->path =~ /\/-\//;
+ return $c->uri($orig);
+ }
+ );
+ $finder->find( \$c->res->{body} ) if $c->res->body;
}
}
return $c->NEXT::finalize(@_);
}
-=head3 prepare_action
+=item prepare_action
=cut
}
}
-=head3 setup
+=item setup
+
+Sets up the session cache file.
=cut
sub setup {
- my $self = shift;
- my $cache_root = $self->config->{cache_root} || tempdir;
- my $default_expires_in = $self->config->{default_expires_in}
- || 60 * 60 * 24;
- my $auto_purge_interval = $self->config->{auto_purge_interval}
- || 60 * 60 * 24;
- my $auto_purge_on_set = $self->config->{auto_purge_on_set} || 1;
+ my $self = shift;
+ $self->config->{session}->{storage} ||= '/tmp/session';
+ $self->config->{session}->{expires} ||= 60 * 60 * 24;
+ $self->config->{session}->{rewrite} ||= 0;
+
$self->_session(
Cache::FastMmap->new(
- cache_root => $cache_root,
- default_expires_in => $default_expires_in,
- auto_purge_interval => $auto_purge_interval,
- auto_purge_on_set => $auto_purge_on_set
+ share_file => $self->config->{session}->{storage},
+ expire_time => $self->config->{session}->{expires}
)
);
+
return $self->NEXT::setup(@_);
}
+=back
+
=head2 METHODS
-=head3 session
+=over 4
-=head3 uri
+=item session
+
+=item uri
Extends an uri with session id if needed.
return $uri;
}
+=back
+
=head2 CONFIG OPTIONS
-=head3 no_url_rewrite
+=over 4
-To disable automatic storing of sessions in the url,
-you can disable the url rewriting for session by setting
-this to a true value.
+=item rewrite
-=head3 cache_root
+If set to a true value sessions are automatically stored in the url;
+defaults to false.
-The root directory for the session cache. defaults to a
-tempdir.
+=item storage
-=head3 default_expires_in
+Specifies the file to be used for the sharing of session data;
+defaults to C</tmp/session>.
-how many seconds until the session expires. defaults to 1 day
+Note that the file will be created with mode 0640, which means that it
+will only be writeable by processes running with the same uid as the
+process that creates the file. If this may be a problem, for example
+if you may try to debug the program as one user and run it as another,
+specify a filename like C<< /tmp/session-$> >>, which includes the
+UID of the process in the filename.
-=head3 auto_purge_interval
-How often should the system purge sessions. Defaults to 1 time
-per day.
+=item expires
-=head3 auto_purge_on_set
+Specifies the session expiry time in seconds; defaults to 86,400,
+i.e. one day.
-Is auto purge enabled? defaults to true.
+=back
=head1 SEE ALSO
-L<Catalyst> L<Cache::FastMmap>.
+L<Catalyst>, L<Cache::FastMmap>.
=head1 AUTHOR
-Sebastian Riedel, C<sri@cpan.org>
-Marcus Ramberg C<mramberg@cpan.org>
+Sebastian Riedel E<lt>C<sri@cpan.org>E<gt>,
+Marcus Ramberg E<lt>C<mramberg@cpan.org>E<gt>,
+Andrew Ford E<lt>C<andrewf@cpan.org>E<gt>
=head1 COPYRIGHT
-This program is free software, you can redistribute it and/or modify it under
-the same terms as Perl itself.
+This program is free software, you can redistribute it and/or modify it
+under the same terms as Perl itself.
=cut