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.11';
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;
57 $c->response->cookies->{session} = {
59 expires => $c->config->{session}->{expires} . 's'
62 if ( $c->config->{session}->{rewrite} ) {
63 my $finder = URI::Find->new(
65 my ( $uri, $orig ) = @_;
66 my $base = $c->request->base;
67 return $orig unless $orig =~ /^$base/;
68 return $orig if $uri->path =~ /\/-\//;
69 return $c->uri($orig);
72 $finder->find( \$c->res->{body} ) if $c->res->body;
75 return $c->NEXT::finalize(@_);
84 if ( $c->request->path =~ /^(.*)\/\-\/(.+)$/ ) {
85 $c->request->path($1);
87 $c->log->debug(qq/Found sessionid "$2" in path/) if $c->debug;
89 if ( my $cookie = $c->request->cookies->{session} ) {
90 my $sid = $cookie->value;
92 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
94 $c->NEXT::prepare_action(@_);
99 return $c->{session} if $c->{session};
100 my $sid = $c->sessionid;
103 && ( $c->{session} = $c->_session->get($sid) ) )
105 $c->log->debug(qq/Found session "$sid"/) if $c->debug;
106 return $c->{session};
109 my $sid = Digest::MD5::md5_hex( time, rand, $$, 'catalyst' );
111 $c->log->debug(qq/Created session "$sid"/) if $c->debug;
112 return $c->{session} = {};
122 $self->config->{session}->{storage} ||= '/tmp/session';
123 $self->config->{session}->{expires} ||= 60 * 60 * 24;
124 $self->config->{session}->{rewrite} ||= 0;
127 Cache::FastMmap->new(
128 share_file => $self->config->{session}->{storage},
129 expire_time => $self->config->{session}->{expires}
133 return $self->NEXT::setup(@_);
142 Extends an uri with session id if needed.
144 my $uri = $c->uri('http://localhost/foo');
149 my ( $c, $uri ) = @_;
150 if ( my $sid = $c->sessionid ) {
151 $uri = URI->new($uri);
152 my $path = $uri->path;
153 $path .= '/' unless $path =~ /\/$/;
154 $uri->path( $path . "-/$sid" );
155 return $uri->as_string;
160 =head2 CONFIG OPTIONS
164 To enable automatic storing of sessions in the url set this to a true value.
168 File to mmap for sharing of data, defaults to /tmp/session.
172 how many seconds until the session expires. defaults to 1 day
176 L<Catalyst> L<Cache::FastMmap>.
180 Sebastian Riedel, C<sri@cpan.org>
181 Marcus Ramberg C<mramberg@cpan.org>
185 This program is free software, you can redistribute it and/or modify it
186 under the same terms as Perl itself.