| 1 | package Plack::Session::Store::RedisFast; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use 5.008_005; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Carp qw( carp ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Plack::Util::Accessor qw( prefix redis encoder expires ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Time::Seconds qw( ONE_MONTH ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use parent 'Plack::Session::Store'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use constant SESSIONS_PER_SCAN => 100; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 17 | 2 | our $AUTHORITY = 'cpan:AKZHAN'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=28.8
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 2 | my ( $class, %param ) = @_; | |||||||||||||||||||||||||||
| 21 | 2 | ▶ | $param{prefix} = __PACKAGE__ . ':' unless defined $param{prefix}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | if ( exists $param{expire} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | warn __PACKAGE__ | |||||||||||||||||||||||||||
| 24 | . ": DEPRECATED expire. Should be replaced with expires."; | ||||||||||||||||||||||||||||
| 25 | 0 | $param{expires} = delete $param{expire}; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | 2 | ▶ | $param{expires} = ONE_MONTH unless exists $param{expires}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | unless ( defined $param{redis} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 2 | $param{redis} = \&_build_redis; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 2 | ▶ | if ( ref( $param{redis} ) eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 2 | $param{redis} = $param{redis}->(); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | $param{encoder} ||= | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | _build_encoder( ( delete $param{inflate} ), ( delete $param{deflate} ) ); | ||||||||||||||||||||||||||
|
Condition0%0/3
$param{"encoder"} ||= _build_encoder(delete $param{"inflate"}, delete $param{"deflate"})
MC/DC0%0/2
$param{"encoder"} ||= _build_encoder(delete $param{"inflate"}, delete $param{"deflate"})
$param{"encoder"}
_build_encoder(delete $param{"inflate"}, delete $param{"deflate"})
Truth table0%0/3
A ||= B
A= $param{"encoder"}B= _build_encoder(delete $param{"inflate"}, delete $param{"deflate"})
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | $param{encoder} = $param{encoder}->new() | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | unless ref( $param{encoder} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | bless {%param} => $class; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 2 | ▶ | sub _build_redis { | ||||||||||||||||||||||||||
|
Subroutine
_build_redis: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 47 | 2 | my $instance; | |||||||||||||||||||||||||||
| 48 | eval { | ||||||||||||||||||||||||||||
| 49 | 2 | require Redis::Fast; | |||||||||||||||||||||||||||
| 50 | 2 | $instance = Redis::Fast->new; | |||||||||||||||||||||||||||
| 51 | 0 | 1; | |||||||||||||||||||||||||||
| 52 | 2 | ▶ | } or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 2 | require Redis; | |||||||||||||||||||||||||||
| 54 | 0 | $instance = Redis->new; | |||||||||||||||||||||||||||
| 55 | }; | ||||||||||||||||||||||||||||
| 56 | 0 | $instance; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | sub _build_encoder { | ||||||||||||||||||||||||||
|
Subroutine
_build_encoder: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 60 | 0 | my ( $inflate, $deflate ) = @_; | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ( $inflate && $deflate ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$inflate and $deflate
MC/DC0%0/2
$inflate and $deflate
$inflate
$deflate
Truth table0%0/3
A and B
A= $inflateB= $deflate
| |||||||||||||||||||||||||||||
| 62 | 0 | require Plack::Session::Store::RedisFast::Encoder::Custom; | |||||||||||||||||||||||||||
| 63 | 0 | return Plack::Session::Store::RedisFast::Encoder::Custom->new( $inflate, | |||||||||||||||||||||||||||
| 64 | $deflate ); | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 0 | my $instance; | |||||||||||||||||||||||||||
| 67 | eval { | ||||||||||||||||||||||||||||
| 68 | 0 | require Plack::Session::Store::RedisFast::Encoder::JSONXS; | |||||||||||||||||||||||||||
| 69 | 0 | $instance = Plack::Session::Store::RedisFast::Encoder::JSONXS->new; | |||||||||||||||||||||||||||
| 70 | 0 | 1; | |||||||||||||||||||||||||||
| 71 | } or do { | ||||||||||||||||||||||||||||
| 72 | 0 | require Plack::Session::Store::RedisFast::Encoder::MojoJSON; | |||||||||||||||||||||||||||
| 73 | 0 | $instance = Plack::Session::Store::RedisFast::Encoder::MojoJSON->new; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
eval {
do {
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
}
MC/DC0%0/2
eval {
do {
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
}
eval {
do {
require Plack::Session::Store::RedisFast::Encoder::JSONXS;
$instance = "Plack::Session::Store::RedisFast::Encoder::JSONXS"->new;
1
}
}
do {
require Plack::Session::Store::RedisFast::Encoder::MojoJSON;
$instance = "Plack::Session::Store::RedisFast::Encoder::MojoJSON"->new
}
Truth table0%0/3
A or B
A= eval {
do {
require Plack::Session::Store::RedisFast::Encoder::JSONXS;
$instance = "Plack::Session::Store::RedisFast::Encoder::JSONXS"->new;
1
}
}B= do {
require Plack::Session::Store::RedisFast::Encoder::MojoJSON;
$instance = "Plack::Session::Store::RedisFast::Encoder::MojoJSON"->new
}
| |||||||||||||||||||||||||||||
| 76 | 0 | require Plack::Session::Store::RedisFast::Encoder::JSON; | |||||||||||||||||||||||||||
| 77 | 0 | $instance = Plack::Session::Store::RedisFast::Encoder::JSON->new; | |||||||||||||||||||||||||||
| 78 | }; | ||||||||||||||||||||||||||||
| 79 | 0 | $instance; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 0 | my ( $self, $session_id ) = @_; | |||||||||||||||||||||||||||
| 84 | 0 | my $data = $self->redis->get( $self->prefix . $session_id ); | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | return undef unless defined $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | $self->encoder->decode($data); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub store { | ||||||||||||||||||||||||||
|
Subroutine
store: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 0 | my ( $self, $session_id, $session ) = @_; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | unless ( defined $session ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | carp "store: no session provided"; | |||||||||||||||||||||||||||
| 93 | 0 | return; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 0 | my $data = $self->encoder->encode($session); | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | $self->redis->set( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | $self->prefix . $session_id => $data, | ||||||||||||||||||||||||||||
| 98 | ( defined( $self->expires ) ? ( EX => $self->expires ) : () ), | ||||||||||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||||||||||
| 100 | 0 | 1; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub remove { | ||||||||||||||||||||||||||
|
Subroutine
remove: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 0 | my ( $self, $session_id ) = @_; | |||||||||||||||||||||||||||
| 105 | 0 | $self->redis->del( $self->prefix . $session_id ); | |||||||||||||||||||||||||||
| 106 | 0 | 1; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub each_session { | ||||||||||||||||||||||||||
|
Subroutine
each_session: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 0 | my ( $self, $cb ) = @_; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | return if ref($cb) ne 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | my $prefix = $self->prefix; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | my $cursor = 0; | |||||||||||||||||||||||||||
| 116 | 0 | for ( ; ; ) { | |||||||||||||||||||||||||||
| 117 | 0 | ( $cursor, my $keys ) = $self->redis->scan( | |||||||||||||||||||||||||||
| 118 | $cursor, | ||||||||||||||||||||||||||||
| 119 | MATCH => $self->prefix . '*', | ||||||||||||||||||||||||||||
| 120 | COUNT => SESSIONS_PER_SCAN | ||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ( scalar(@$keys) > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | my @sessions = $self->redis->mget(@$keys); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | for ( my $i = 0 ; $i < scalar(@sessions) ; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | next unless $sessions[$i]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | next if $keys->[$i] !~ m/^\Q$prefix\E(.+)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | my $session_id = $1; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | $cb->( | |||||||||||||||||||||||||||
| 132 | $self->redis, $prefix, $session_id, | ||||||||||||||||||||||||||||
| 133 | $self->encoder->decode( $sessions[$i] ), | ||||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | last if $cursor == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 0 | 1; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 2 | 1; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | __END__ | ||||||||||||||||||||||||||||