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 $c->session->{foo} = 'bar';
32 =head2 EXTENDED METHODS
40 unless ($c->config->{no_url_rewrite}) {
41 my $redirect = $c->response->redirect;
42 $c->response->redirect( $c->uri($redirect) ) if $redirect;
44 if ( my $sid = $c->sessionid ) {
45 $c->_session->set( $sid, $c->session );
47 if ( my $cookie = $c->request->cookies->{session} ) {
48 $set = 0 if $cookie->value eq $sid;
50 $c->response->cookies->{session} = { value => $sid } if $set;
51 unless ($c->config->{no_url_rewrite}) {
52 my $finder = URI::Find->new(
54 my ( $uri, $orig ) = @_;
55 my $base = $c->request->base;
56 return $orig unless $orig =~ /^$base/;
57 return $orig if $uri->path =~ /\/-\//;
58 return $c->uri($orig);
61 $finder->find( \$c->res->{output} ) if $c->res->output;
64 return $c->NEXT::finalize(@_);
73 if ( $c->request->path =~ /^(.*)\/\-\/(.+)$/ ) {
74 $c->request->path($1);
76 $c->log->debug(qq/Found sessionid "$2" in path/) if $c->debug;
78 if ( my $cookie = $c->request->cookies->{session} ) {
79 my $sid = $cookie->value;
81 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
83 $c->NEXT::prepare_action(@_);
88 return $c->{session} if $c->{session};
89 my $sid = $c->sessionid;
92 && ( $c->{session} = $c->_session->get($sid) ) )
94 $c->log->debug(qq/Found session "$sid"/) if $c->debug;
98 my $sid = Digest::MD5::md5_hex( time, rand, $$, 'catalyst' );
100 $c->log->debug(qq/Created session "$sid"/) if $c->debug;
101 return $c->{session} = {};
111 my $cache_root = $self->config->{cache_root} || tempdir;
112 my $default_expires_in = $self->config->{default_expires_in}
114 my $auto_purge_interval = $self->config->{auto_purge_interval}
116 my $auto_purge_on_set = $self->config->{auto_purge_on_set} || 1;
118 Cache::FastMmap->new(
119 cache_root => $cache_root,
120 default_expires_in => $default_expires_in,
121 auto_purge_interval => $auto_purge_interval,
122 auto_purge_on_set => $auto_purge_on_set
125 return $self->NEXT::setup(@_);
134 Extends an uri with session id if needed.
136 my $uri = $c->uri('http://localhost/foo');
141 my ( $c, $uri ) = @_;
142 if ( my $sid = $c->sessionid ) {
143 $uri = URI->new($uri);
144 my $path = $uri->path;
145 $path .= '/' unless $path =~ /\/$/;
146 $uri->path( $path . "-/$sid" );
147 return $uri->as_string;
152 =head2 CONFIG OPTIONS
154 =head3 no_url_rewrite
156 To disable automatic storing of sessions in the url,
157 you can disable the url rewriting for session by setting
158 this to a true value.
162 The root directory for the session cache. defaults to a
165 =head3 default_expires_in
167 how many seconds until the session expires. defaults to 1 day
169 =head3 auto_purge_interval
171 How often should the system purge sessions. Defaults to 1 time
174 =head3 auto_purge_on_set
176 Is auto purge enabled? defaults to true.
180 L<Catalyst> L<Cache::FastMmap>.
184 Sebastian Riedel, C<sri@cpan.org>
185 Marcus Ramberg C<mramberg@cpan.org>
189 This program is free software, you can redistribute it and/or modify it under
190 the same terms as Perl itself.