File Coverage

blib/lib/Dancer/Session/Redis.pm
Criterion Covered Total %
statement 32 56 57.1
branch 5 14 35.7
condition 9 21 42.8
subroutine 9 13 69.2
pod 6 6 100.0
total 61 110 55.4


line stmt bran cond sub pod time code
1             package Dancer::Session::Redis;
2              
3             # ABSTRACT: Redis backend for Dancer Session Engine
4              
5 2     2   274074 use strict;
  2         4  
  2         75  
6 2     2   12 use warnings;
  2         3  
  2         67  
7 2     2   11 use parent 'Dancer::Session::Abstract';
  2         4  
  2         18  
8 2     2   157524 use Redis 1.955;
  2         75786  
  2         88  
9 2     2   229 use Dancer::Config 'setting';
  2         9  
  2         263  
10 2     2   14 use Storable ();
  2         6  
  2         36  
11 2     2   13 use Carp ();
  2         3  
  2         1477  
12              
13             our $VERSION = '0.22'; # VERSION
14             our $AUTHORITY = 'cpan:CHIM'; # AUTHORITY
15              
16             my $_redis;
17             my %options = ();
18              
19             sub init {
20 3     3 1 24 my $self = shift;
21              
22 3         20 $self->SUPER::init(@_);
23              
24             # backend settings
25 3 100       659 if (my $opts = setting('redis_session') ) {
26 2 100 66     30 if (ref $opts and ref $opts eq 'HASH' ) {
27 1   50     60 %options = (
      50        
      50        
      50        
      50        
      50        
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             );
35             }
36             else {
37 1         11 Carp::croak 'Settings redis_session must be a hash reference!';
38             }
39             }
40             else {
41 1         37 Carp::croak 'Settings redis_session is not defined!';
42             }
43              
44 1 50 33     11 unless (defined $options{server} || defined $options{sock}) {
45 1         16 Carp::croak 'Settings redis_session should include either server or sock parameter!';
46             }
47              
48             # get radis handle
49 0         0 $self->redis;
50             }
51              
52             # create a new session
53             sub create {
54 3     3 1 3897 my ($class) = @_;
55              
56 3         30 $class->new->flush;
57             }
58              
59             # fetch the session object by id
60             sub retrieve($$) {
61 0     0 1   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             sub destroy {
72 0     0 1   my ($self) = @_;
73              
74 0           $self->redis->select($options{database});
75 0           $self->redis->del($self->id);
76             }
77              
78             # flush session
79             sub flush {
80 0     0 1   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             sub redis {
91 0     0 1   my ($self) = @_;
92              
93 0 0 0       if (!$_redis || !$_redis->ping) {
94 0           my %params = (
95             debug => $options{debug},
96             reconnect => 10,
97             every => 100,
98             );
99              
100 0 0         if (defined $options{sock}) {
101 0           $params{sock} = $options{sock};
102             }
103             else {
104 0           $params{server} = $options{server};
105             }
106              
107 0 0         $params{password} = $options{password} if $options{password};
108              
109 0           $_redis = Redis->new(%params);
110             }
111              
112 0 0         $_redis and return $_redis;
113              
114 0           Carp::croak "Unable connect to redis-server...";
115             }
116              
117             1; # End of Dancer::Session::Redis
118              
119             __END__