Plack-Session-Store-RedisFast
view release on metacpan or search on metacpan
lib/Plack/Session/Store/RedisFast.pm view on Meta::CPAN
package Plack::Session::Store::RedisFast;
use strict;
use warnings;
use 5.008_005;
use Carp qw( carp );
use Plack::Util::Accessor qw( prefix redis encoder expires );
use Time::Seconds qw( ONE_MONTH );
use parent 'Plack::Session::Store';
use constant SESSIONS_PER_SCAN => 100;
our $VERSION = '0.05';
our $AUTHORITY = 'cpan:AKZHAN';
sub new {
my ( $class, %param ) = @_;
$param{prefix} = __PACKAGE__ . ':' unless defined $param{prefix};
if ( exists $param{expire} ) {
warn __PACKAGE__
. ": DEPRECATED expire. Should be replaced with expires.";
$param{expires} = delete $param{expire};
}
$param{expires} = ONE_MONTH unless exists $param{expires};
unless ( defined $param{redis} ) {
$param{redis} = \&_build_redis;
}
if ( ref( $param{redis} ) eq 'CODE' ) {
$param{redis} = $param{redis}->();
}
$param{encoder} ||=
_build_encoder( ( delete $param{inflate} ), ( delete $param{deflate} ) );
$param{encoder} = $param{encoder}->new()
unless ref( $param{encoder} );
bless {%param} => $class;
}
sub _build_redis {
my $instance;
eval {
require Redis::Fast;
$instance = Redis::Fast->new;
1;
} or do {
require Redis;
$instance = Redis->new;
};
$instance;
}
sub _build_encoder {
my ( $inflate, $deflate ) = @_;
if ( $inflate && $deflate ) {
require Plack::Session::Store::RedisFast::Encoder::Custom;
return Plack::Session::Store::RedisFast::Encoder::Custom->new( $inflate,
$deflate );
}
my $instance;
eval {
require Plack::Session::Store::RedisFast::Encoder::JSONXS;
$instance = Plack::Session::Store::RedisFast::Encoder::JSONXS->new;
1;
} or do {
require Plack::Session::Store::RedisFast::Encoder::MojoJSON;
$instance = Plack::Session::Store::RedisFast::Encoder::MojoJSON->new;
}
or do {
require Plack::Session::Store::RedisFast::Encoder::JSON;
$instance = Plack::Session::Store::RedisFast::Encoder::JSON->new;
};
$instance;
}
sub fetch {
my ( $self, $session_id ) = @_;
my $data = $self->redis->get( $self->prefix . $session_id );
return undef unless defined $data;
$self->encoder->decode($data);
}
sub store {
my ( $self, $session_id, $session ) = @_;
unless ( defined $session ) {
carp "store: no session provided";
return;
}
my $data = $self->encoder->encode($session);
$self->redis->set(
$self->prefix . $session_id => $data,
( defined( $self->expires ) ? ( EX => $self->expires ) : () ),
);
1;
}
sub remove {
( run in 1.228 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )