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.06';
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 my $redirect = $c->response->redirect;
41 $c->response->redirect( $c->uri($redirect) ) if $redirect;
42 if ( my $sid = $c->sessionid ) {
43 $c->_session->set( $sid, $c->session );
45 if ( my $cookie = $c->request->cookies->{session} ) {
46 $set = 0 if $cookie->value eq $sid;
48 $c->response->cookies->{session} = { value => $sid } if $set;
49 unless ($c->config->{no_url_rewrite}) {
50 my $finder = URI::Find->new(
52 my ( $uri, $orig ) = @_;
53 my $base = $c->request->base;
54 return $orig unless $orig =~ /^$base/;
55 return $orig if $uri->path =~ /\/-\//;
56 return $c->uri($orig);
59 $finder->find( \$c->res->{output} ) if $c->res->output;
62 return $c->NEXT::finalize(@_);
71 if ( $c->request->path =~ /^(.*)\/\-\/(.+)$/ ) {
72 $c->request->path($1);
74 $c->log->debug(qq/Found sessionid "$2" in path/) if $c->debug;
76 if ( my $cookie = $c->request->cookies->{session} ) {
77 my $sid = $cookie->value;
79 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
81 $c->NEXT::prepare_action(@_);
86 return $c->{session} if $c->{session};
87 my $sid = $c->sessionid;
90 && ( $c->{session} = $c->_session->get($sid) ) )
92 $c->log->debug(qq/Found session "$sid"/) if $c->debug;
96 my $sid = Digest::MD5::md5_hex( time, rand, $$, 'catalyst' );
98 $c->log->debug(qq/Created session "$sid"/) if $c->debug;
99 return $c->{session} = {};
109 my $cache_root = $self->config->{cache_root} || tempdir;
110 my $default_expires_in = $self->config->{default_expires_in}
112 my $auto_purge_interval = $self->config->{auto_purge_interval}
114 my $auto_purge_on_set = $self->config->{auto_purge_on_set} || 1;
116 Cache::FastMmap->new(
117 cache_root => $cache_root,
118 default_expires_in => $default_expires_in,
119 auto_purge_interval => $auto_purge_interval,
120 auto_purge_on_set => $auto_purge_on_set
123 return $self->NEXT::setup(@_);
132 Extends an uri with session id if needed.
134 my $uri = $c->uri('http://localhost/foo');
139 my ( $c, $uri ) = @_;
140 if ( my $sid = $c->sessionid ) {
141 $uri = URI->new($uri);
142 my $path = $uri->path;
143 $path .= '/' unless $path =~ /\/$/;
144 $uri->path( $path . "-/$sid" );
145 return $uri->as_string;
150 =head2 CONFIG OPTIONS
152 =head3 no_url_rewrite
154 To disable automatic storing of sessions in the url,
155 you can disable the url rewriting for session by setting
156 this to a true value.
160 The root directory for the session cache. defaults to a
163 =head3 default_expires_in
165 how many seconds until the session expires. defaults to 1 day
167 =head3 auto_purge_interval
169 How often should the system purge sessions. Defaults to 1 time
172 =head3 auto_purge_on_set
174 Is auto purge enabled? defaults to true.
178 L<Catalyst> L<Cache::FastMmap>.
182 Sebastian Riedel, C<sri@cpan.org>
183 Marcus Ramberg C<mramberg@cpan.org>
187 This program is free software, you can redistribute it and/or modify it under
188 the same terms as Perl itself.