1 package Catalyst::Plugin::Session::FastMmap;
4 use base qw/Class::Data::Inheritable Class::Accessor::Fast/;
10 use File::Temp 'tempdir';
12 our $VERSION = '0.07';
14 __PACKAGE__->mk_classdata('_session');
15 __PACKAGE__->mk_accessors('sessionid');
19 Catalyst::Plugin::Session::FastMmap - FastMmap sessions for Catalyst
23 use Catalyst 'Session::FastMmap';
25 MyApp->config->{session} = {
28 storage => '/tmp/session'
31 $c->session->{foo} = 'bar';
38 =head2 EXTENDED METHODS
46 if ( $c->config->{session}->{rewrite} ) {
47 my $redirect = $c->response->redirect;
48 $c->response->redirect( $c->uri($redirect) ) if $redirect;
50 if ( my $sid = $c->sessionid ) {
51 $c->_session->set( $sid, $c->session );
53 if ( my $cookie = $c->request->cookies->{session} ) {
54 $set = 0 if $cookie->value eq $sid;
56 $c->response->cookies->{session} = { value => $sid } if $set;
57 if ( $c->config->{session}->{rewrite} ) {
58 my $finder = URI::Find->new(
60 my ( $uri, $orig ) = @_;
61 my $base = $c->request->base;
62 return $orig unless $orig =~ /^$base/;
63 return $orig if $uri->path =~ /\/-\//;
64 return $c->uri($orig);
67 $finder->find( \$c->res->{output} ) if $c->res->output;
70 return $c->NEXT::finalize(@_);
79 if ( $c->request->path =~ /^(.*)\/\-\/(.+)$/ ) {
80 $c->request->path($1);
82 $c->log->debug(qq/Found sessionid "$2" in path/) if $c->debug;
84 if ( my $cookie = $c->request->cookies->{session} ) {
85 my $sid = $cookie->value;
87 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
89 $c->NEXT::prepare_action(@_);
94 return $c->{session} if $c->{session};
95 my $sid = $c->sessionid;
98 && ( $c->{session} = $c->_session->get($sid) ) )
100 $c->log->debug(qq/Found session "$sid"/) if $c->debug;
101 return $c->{session};
104 my $sid = Digest::MD5::md5_hex( time, rand, $$, 'catalyst' );
106 $c->log->debug(qq/Created session "$sid"/) if $c->debug;
107 return $c->{session} = {};
117 $self->config->{session}->{storage} ||= '/tmp/session';
118 $self->config->{session}->{expires} ||= 60 * 60 * 24;
119 $self->config->{session}->{rewrite} ||= 0;
122 Cache::FastMmap->new(
123 share_file => $self->config->{session}->{storage},
124 expire_time => $self->config->{session}->{expires}
128 return $self->NEXT::setup(@_);
137 Extends an uri with session id if needed.
139 my $uri = $c->uri('http://localhost/foo');
144 my ( $c, $uri ) = @_;
145 if ( my $sid = $c->sessionid ) {
146 $uri = URI->new($uri);
147 my $path = $uri->path;
148 $path .= '/' unless $path =~ /\/$/;
149 $uri->path( $path . "-/$sid" );
150 return $uri->as_string;
155 =head2 CONFIG OPTIONS
159 To enable automatic storing of sessions in the url set this to a true value.
163 File to mmap for sharing of data, defaults to /tmp/session.
167 how many seconds until the session expires. defaults to 1 day
171 L<Catalyst> L<Cache::FastMmap>.
175 Sebastian Riedel, C<sri@cpan.org>
176 Marcus Ramberg C<mramberg@cpan.org>
180 This program is free software, you can redistribute it and/or modify it under
181 the same terms as Perl itself.