| 1 | package Dancer::Session::Redis; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Redis backend for Dancer Session Engine | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use parent 'Dancer::Session::Abstract'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Redis 1.955; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Dancer::Config 'setting'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Storable (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | our $VERSION = '0.22'; # VERSION | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | our $AUTHORITY = 'cpan:CHIM'; # AUTHORITY | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | my $_redis; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | my %options = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=12 SCAR=28.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | $self->SUPER::init(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # backend settings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | ▶ | if (my $opts = setting('redis_session') ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | ▶ | if (ref $opts and ref $opts eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $opts and ref $opts eq "HASH"
MC/DC50%1/2
ref $opts and ref $opts eq "HASH"
ref $opts
ref $opts eq "HASH"
Truth table66%2/3
A and B
A= ref $optsB= ref $opts eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | %options = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | server => $opts->{server} || undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | sock => $opts->{sock} || undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | database => $opts->{database} || 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | expire => $opts->{expire} || 900, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | debug => $opts->{debug} || 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | password => $opts->{password} || undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%6/12
$opts->{'server'} || undef
$opts->{'sock'} || undef
$opts->{'database'} || 0
$opts->{'expire'} || 900
$opts->{'debug'} || 0
$opts->{'password'} || undef
MC/DC0%0/1
$opts->{'server'} || undef
$opts->{'server'}
MC/DC0%0/1
$opts->{'sock'} || undef
$opts->{'sock'}
MC/DC0%0/1
$opts->{'database'} || 0
$opts->{'database'}
MC/DC0%0/1
$opts->{'expire'} || 900
$opts->{'expire'}
MC/DC0%0/1
$opts->{'debug'} || 0
$opts->{'debug'}
MC/DC0%0/1
$opts->{'password'} || undef
$opts->{'password'}
Truth table50%1/2
A
A= $opts->{'server'}
Truth table50%1/2
A
A= $opts->{'sock'}
Truth table50%1/2
A
A= $opts->{'database'}
Truth table50%1/2
A
A= $opts->{'expire'}
Truth table50%1/2
A
A= $opts->{'debug'}
Truth table50%1/2
A
A= $opts->{'password'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | Carp::croak 'Settings redis_session must be a hash reference!'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | Carp::croak 'Settings redis_session is not defined!'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | ▶ | unless (defined $options{server} || defined $options{sock}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $options{"server"} or defined $options{"sock"}
MC/DC0%0/2
defined $options{"server"} or defined $options{"sock"}
defined $options{"server"}
defined $options{"sock"}
Truth table33%1/3
A or B
A= defined $options{"server"}B= defined $options{"sock"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | Carp::croak 'Settings redis_session should include either server or sock parameter!'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # get radis handle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | $self->redis; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # create a new session | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | ▶ | sub create { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | my ($class) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 3 | $class->new->flush; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # fetch the session object by id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub retrieve($$) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
retrieve: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my ($class, $id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my $self = $class->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $self->redis->select($options{database}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $self->redis->expire($id => $options{expire}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | Storable::thaw($self->redis->get($id)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # delete session | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub destroy { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
destroy: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $self->redis->select($options{database}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $self->redis->del($self->id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # flush session | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub flush { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flush: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $self->redis->select($options{database}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | $self->redis->set($self->id => Storable::freeze($self)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $self->redis->expire($self->id => $options{expire}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # get redis handle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub redis { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
redis: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if (!$_redis || !$_redis->ping) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_redis and $_redis->ping
MC/DC0%0/2
$_redis and $_redis->ping
$_redis
$_redis->ping
Truth table0%0/3
A and B
A= $_redisB= $_redis->ping
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | my %params = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | debug => $options{debug}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | reconnect => 10, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | every => 100, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if (defined $options{sock}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | $params{sock} = $options{sock}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | $params{server} = $options{server}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | $params{password} = $options{password} if $options{password}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | $_redis = Redis->new(%params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | $_redis and return $_redis; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | Carp::croak "Unable connect to redis-server..."; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | 1; # End of Dancer::Session::Redis | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||