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.09';
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} = {
58 expires => "+" . $self->config->{session}->{expires} . "s"
61 if ( $c->config->{session}->{rewrite} ) {
62 my $finder = URI::Find->new(
64 my ( $uri, $orig ) = @_;
65 my $base = $c->request->base;
66 return $orig unless $orig =~ /^$base/;
67 return $orig if $uri->path =~ /\/-\//;
68 return $c->uri($orig);
71 $finder->find( \$c->res->{body} ) if $c->res->body;
74 return $c->NEXT::finalize(@_);
83 if ( $c->request->path =~ /^(.*)\/\-\/(.+)$/ ) {
84 $c->request->path($1);
86 $c->log->debug(qq/Found sessionid "$2" in path/) if $c->debug;
88 if ( my $cookie = $c->request->cookies->{session} ) {
89 my $sid = $cookie->value;
91 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
93 $c->NEXT::prepare_action(@_);
98 return $c->{session} if $c->{session};
99 my $sid = $c->sessionid;
102 && ( $c->{session} = $c->_session->get($sid) ) )
104 $c->log->debug(qq/Found session "$sid"/) if $c->debug;
105 return $c->{session};
108 my $sid = Digest::MD5::md5_hex( time, rand, $$, 'catalyst' );
110 $c->log->debug(qq/Created session "$sid"/) if $c->debug;
111 return $c->{session} = {};
121 $self->config->{session}->{storage} ||= '/tmp/session';
122 $self->config->{session}->{expires} ||= 60 * 60 * 24;
123 $self->config->{session}->{rewrite} ||= 0;
126 Cache::FastMmap->new(
127 share_file => $self->config->{session}->{storage},
128 expire_time => $self->config->{session}->{expires}
132 return $self->NEXT::setup(@_);
141 Extends an uri with session id if needed.
143 my $uri = $c->uri('http://localhost/foo');
148 my ( $c, $uri ) = @_;
149 if ( my $sid = $c->sessionid ) {
150 $uri = URI->new($uri);
151 my $path = $uri->path;
152 $path .= '/' unless $path =~ /\/$/;
153 $uri->path( $path . "-/$sid" );
154 return $uri->as_string;
159 =head2 CONFIG OPTIONS
163 To enable automatic storing of sessions in the url set this to a true value.
167 File to mmap for sharing of data, defaults to /tmp/session.
171 how many seconds until the session expires. defaults to 1 day
175 L<Catalyst> L<Cache::FastMmap>.
179 Sebastian Riedel, C<sri@cpan.org>
180 Marcus Ramberg C<mramberg@cpan.org>
184 This program is free software, you can redistribute it and/or modify it
185 under the same terms as Perl itself.