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.13';
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';
36 C<Catalyst::Plugin::Session::FastMmap> is a fast session plugin for
37 Catalyst that uses an mmap'ed file to act as a shared memory
38 interprocess cache. It is based on C<Cache::FastMMap>.
41 =head2 EXTENDED METHODS
51 if ( $c->config->{session}->{rewrite} ) {
52 my $redirect = $c->response->redirect;
53 $c->response->redirect( $c->uri($redirect) ) if $redirect;
55 if ( my $sid = $c->sessionid ) {
56 $c->_session->set( $sid, $c->session );
58 if ( my $cookie = $c->request->cookies->{session} ) {
59 $set = 0 if $cookie->value eq $sid;
62 $c->response->cookies->{session} = {
66 if ( $c->config->{session}->{rewrite} ) {
67 my $finder = URI::Find->new(
69 my ( $uri, $orig ) = @_;
70 my $base = $c->request->base;
71 return $orig unless $orig =~ /^$base/;
72 return $orig if $uri->path =~ /\/-\//;
73 return $c->uri($orig);
76 $finder->find( \$c->res->{body} ) if $c->res->body;
79 return $c->NEXT::finalize(@_);
88 if ( $c->request->path =~ /^(.*)\/\-\/(.+)$/ ) {
89 $c->request->path($1);
91 $c->log->debug(qq/Found sessionid "$2" in path/) if $c->debug;
93 if ( my $cookie = $c->request->cookies->{session} ) {
94 my $sid = $cookie->value;
96 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
98 $c->NEXT::prepare_action(@_);
103 return $c->{session} if $c->{session};
104 my $sid = $c->sessionid;
107 && ( $c->{session} = $c->_session->get($sid) ) )
109 $c->log->debug(qq/Found session "$sid"/) if $c->debug;
110 return $c->{session};
113 my $sid = Digest::MD5::md5_hex( time, rand, $$, 'catalyst' );
115 $c->log->debug(qq/Created session "$sid"/) if $c->debug;
116 return $c->{session} = {};
122 Sets up the session cache file.
128 $self->config->{session}->{storage} ||= '/tmp/session';
129 $self->config->{session}->{expires} ||= 60 * 60 * 24;
130 $self->config->{session}->{rewrite} ||= 0;
133 Cache::FastMmap->new(
134 share_file => $self->config->{session}->{storage},
135 expire_time => $self->config->{session}->{expires}
139 return $self->NEXT::setup(@_);
152 Extends an uri with session id if needed.
154 my $uri = $c->uri('http://localhost/foo');
159 my ( $c, $uri ) = @_;
160 if ( my $sid = $c->sessionid ) {
161 $uri = URI->new($uri);
162 my $path = $uri->path;
163 $path .= '/' unless $path =~ /\/$/;
164 $uri->path( $path . "-/$sid" );
165 return $uri->as_string;
172 =head2 CONFIG OPTIONS
178 If set to a true value sessions are automatically stored in the url;
183 Specifies the file to be used for the sharing of session data;
184 defaults to C</tmp/session>.
186 Note that the file will be created with mode 0640, which means that it
187 will only be writeable by processes running with the same uid as the
188 process that creates the file. If this may be a problem, for example
189 if you may try to debug the program as one user and run it as another,
190 specify a filename like C<< /tmp/session-$> >>, which includes the
191 UID of the process in the filename.
196 Specifies the session expiry time in seconds; defaults to 86,400,
203 L<Catalyst>, L<Cache::FastMmap>.
207 Sebastian Riedel E<lt>C<sri@cpan.org>E<gt>,
208 Marcus Ramberg E<lt>C<mramberg@cpan.org>E<gt>,
209 Andrew Ford E<lt>C<andrewf@cpan.org>E<gt>
213 This program is free software, you can redistribute it and/or modify it
214 under the same terms as Perl itself.