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.05';
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 my $finder = URI::Find->new(
51 my ( $uri, $orig ) = @_;
52 my $base = $c->request->base;
53 return $orig unless $orig =~ /^$base/;
54 return $orig if $uri->path =~ /\/-\//;
55 return $c->uri($orig);
58 $finder->find( \$c->response->{output} );
60 return $c->NEXT::finalize(@_);
69 if ( $c->request->path =~ /^(.*)\/\-\/(.+)$/ ) {
70 $c->request->path($1);
72 $c->log->debug(qq/Found sessionid "$2" in path/) if $c->debug;
74 if ( my $cookie = $c->request->cookies->{session} ) {
75 my $sid = $cookie->value;
77 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
79 $c->NEXT::prepare_action(@_);
84 return $c->{session} if $c->{session};
85 my $sid = $c->sessionid;
86 if ( $sid && $c->_session &&
87 ( $c->{session} = $c->_session->get($sid) ) ) {
88 $c->log->debug(qq/Found session "$sid"/) if $c->debug;
92 my $sid = Digest::MD5::md5_hex( time, rand, $$, 'catalyst' );
94 $c->log->debug(qq/Created session "$sid"/) if $c->debug;
95 return $c->{session} = {};
105 my $cache_root = $self->config->{cache_root} || tempdir;
106 my $default_expires_in = $self->config->{default_expires_in}
108 my $auto_purge_interval = $self->config->{auto_purge_interval}
110 my $auto_purge_on_set = $self->config->{auto_purge_on_set} || 1;
112 Cache::FastMmap->new(
113 cache_root => $cache_root,
114 default_expires_in => $default_expires_in,
115 auto_purge_interval => $auto_purge_interval,
116 auto_purge_on_set => $auto_purge_on_set
119 return $self->NEXT::setup(@_);
128 Extends an uri with session id if needed.
130 my $uri = $c->uri('http://localhost/foo');
135 my ( $c, $uri ) = @_;
136 if ( my $sid = $c->sessionid ) {
137 $uri = URI->new($uri);
138 my $path = $uri->path;
139 $path .= '/' unless $path =~ /\/$/;
140 $uri->path( $path . "-/$sid" );
141 return $uri->as_string;
152 Sebastian Riedel, C<sri@cpan.org>
153 Marcus Ramberg C<mramberg@cpan.org>
157 This program is free software, you can redistribute it and/or modify it under
158 the same terms as Perl itself.