X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Plugin-Session-State-Cookie.git;a=blobdiff_plain;f=FastMmap.pm;h=99cf6793a50f72867ecfba2f060aafae0fd1cd95;hp=5b5e460d18d0859bc396e4dea15f501aa564afdd;hb=c9fb61f355008ebd65bbdaee8c387895c8c24343;hpb=bf2bce672ebec6c0122d133aa86c325e2173c30c diff --git a/FastMmap.pm b/FastMmap.pm index 5b5e460..99cf679 100644 --- a/FastMmap.pm +++ b/FastMmap.pm @@ -9,7 +9,7 @@ use URI; use URI::Find; use File::Temp 'tempdir'; -our $VERSION = '0.04'; +our $VERSION = '0.10'; __PACKAGE__->mk_classdata('_session'); __PACKAGE__->mk_accessors('sessionid'); @@ -21,6 +21,12 @@ Catalyst::Plugin::Session::FastMmap - FastMmap sessions for Catalyst =head1 SYNOPSIS use Catalyst 'Session::FastMmap'; + + MyApp->config->{session} = { + expires => 3600, + rewrite => 1, + storage => '/tmp/session' + }; $c->session->{foo} = 'bar'; print $c->sessionid; @@ -36,26 +42,35 @@ Fast sessions. =cut sub finalize { - my $c = shift; - my $redirect = $c->response->redirect; - $c->response->redirect( $c->uri($redirect) ) if $redirect; + my $c = shift; + if ( $c->config->{session}->{rewrite} ) { + my $redirect = $c->response->redirect; + $c->response->redirect( $c->uri($redirect) ) if $redirect; + } if ( my $sid = $c->sessionid ) { $c->_session->set( $sid, $c->session ); my $set = 1; if ( my $cookie = $c->request->cookies->{session} ) { $set = 0 if $cookie->value eq $sid; } - $c->response->cookies->{session} = { value => $sid } if $set; - 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->response->{output} ); + if ( $set ) { + $c->response->cookies->{session} = { + value => $sid, + expires => $c->config->{session}->{expires} + }; + } + 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(@_); } @@ -83,7 +98,10 @@ sub session { my $c = shift; return $c->{session} if $c->{session}; my $sid = $c->sessionid; - if ( $sid && ( $c->{session} = $c->_session->get($sid) ) ) { + if ( $sid + && $c->_session + && ( $c->{session} = $c->_session->get($sid) ) ) + { $c->log->debug(qq/Found session "$sid"/) if $c->debug; return $c->{session}; } @@ -100,21 +118,18 @@ sub session { =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(@_); } @@ -142,18 +157,33 @@ sub uri { return $uri; } +=head2 CONFIG OPTIONS + +=head3 rewrite + +To enable automatic storing of sessions in the url set this to a true value. + +=head3 storage + +File to mmap for sharing of data, defaults to /tmp/session. + +=head3 expires + +how many seconds until the session expires. defaults to 1 day + =head1 SEE ALSO -L. +L L. =head1 AUTHOR Sebastian Riedel, C +Marcus Ramberg C =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