Created
October 23, 2013 07:27
-
-
Save kazeburo/7114014 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Plack::Middleware::Session::Fast; | |
use strict; | |
use warnings; | |
use parent qw/Plack::Middleware::Session/; | |
use Storable qw//; | |
use Data::MessagePack q//; | |
use Plack::Util::Accessor qw/serialize_method/; | |
sub prepare_app { | |
my $self = shift; | |
$self->serialize_method(\&Storable::freeze) unless $self->serialize_method; | |
$self->serialize_method(\&Storable::freeze) if $self->serialize_method eq 'storable'; | |
$self->serialize_method( | |
sub { Data::MessagePack->pack(+shift) } | |
) if $self->serialize_method eq 'messagepack'; | |
$self->SUPER::prepare_app(); | |
} | |
sub call { | |
my $self = shift; | |
my $env = shift; | |
my($id, $session) = $self->get_session($env); | |
if ($id && $session) { | |
$env->{'psgix.session'} = $session; | |
} else { | |
$id = $self->generate_id($env); | |
$env->{'psgix.session'} = {}; | |
} | |
$env->{'psgix.session.options'} = { | |
id => $id, | |
}; | |
#before app | |
my $freeze_req = $self->serialize_method->([$env->{'psgix.session'},$env->{'psgix.session.options'}]); | |
my $res = $self->app->($env); | |
#after app | |
my $freeze_res = $self->serialize_method->([$env->{'psgix.session'},$env->{'psgix.session.options'}]); | |
return $res if $freeze_req eq $freeze_res; | |
$self->response_cb( | |
$res, sub { | |
$self->finalize($env, $_[0]) | |
} | |
); | |
} | |
1; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment