File Coverage

blib/lib/Dancer/Session/Memcached/Fast.pm
Criterion Covered Total %
statement 27 74 36.4
branch 0 10 0.0
condition 0 9 0.0
subroutine 9 21 42.8
pod 7 7 100.0
total 43 121 35.5


line stmt bran cond sub pod time code
1 1     1   804 use strict;
  1         2  
  1         37  
2 1     1   4 use warnings;
  1         2  
  1         52  
3              
4             package Dancer::Session::Memcached::Fast;
5              
6             # ABSTRACT: Cache::Memcached::Fast based session backend for Dancer
7              
8             our $VERSION = '0.001'; # VERSION
9              
10 1     1   460 use mro;
  1         612  
  1         5  
11 1     1   26 use Carp;
  1         2  
  1         63  
12 1     1   561 use Cache::Memcached::Fast;
  1         4555  
  1         33  
13 1     1   558 use CBOR::XS qw(encode_cbor decode_cbor);
  1         3425  
  1         65  
14 1     1   486 use Dancer::Config qw(setting);
  1         52475  
  1         66  
15 1     1   631 use Dancer qw(config);
  1         124809  
  1         6  
16 1     1   1317 use parent 'Dancer::Session::Abstract';
  1         2  
  1         7  
17              
18             my $setting_prefix = 'session_memcached_fast_';
19              
20             sub _setting {
21 0     0     setting( $setting_prefix . shift(), @_ );
22             }
23              
24             sub init {
25 0     0 1   my $self = shift;
26              
27 0           $self->next::method(@_);
28              
29 0           my $servers = _setting('servers');
30 0 0         croak "The setting session_memcached_servers must be defined"
31             unless defined $servers;
32              
33 0           $servers = [ split /,/, $servers ];
34              
35 0           $self->{cmf} = Cache::Memcached::Fast->new(
36             {
37             servers => $servers,
38             check_args => '',
39             }
40             );
41              
42 0           $self;
43             }
44              
45             sub _engine {
46 0     0     Dancer::engine('session');
47             }
48              
49             sub _mkns {
50 0     0     my $id = shift;
51 0   0       my $ns = _setting('namespace') || config->{appname};
52 0           return "$ns#$id";
53             }
54              
55             sub _boot {
56 0     0     my ( $class, %config ) = @_;
57 0           my $engine = _engine;
58 0           bless {
59             id => undef,
60             cmf => $engine->{cmf},
61             %config
62             } => $class;
63             }
64              
65             sub _set_expire {
66 0     0     my ( $self, $expire ) = @_;
67 0   0       $expire //= setting('session_expires');
68 0 0         if ($expire) {
69 0 0         if ( $expire !~ m{^\d+$} ) {
70 0           $expire = Dancer::Cookie::_parse_duration($expire);
71             }
72 0           $expire -= time;
73             }
74             else {
75 0           $expire = undef;
76             }
77 0           $self->{cmf}->set( '' => time, $expire );
78             }
79              
80             sub create {
81 0     0 1   my ($class) = @_;
82 0           my $self = $class->_boot( id => $class->build_id );
83 0           $self->{cmf}->namespace( _mkns( $self->id ) );
84 0           $self->_set_expire;
85 0           $self;
86             }
87              
88             sub retrieve {
89 0     0 1   my ( $class, $id ) = @_;
90 0           my $self = $class->_boot( id => $id );
91 0           $self->{cmf}->namespace( _mkns( $self->id ) );
92 0           my $time = $self->{cmf}->get('');
93 0 0 0       return unless defined $time and $time =~ m{^\d+$};
94 0           $self->_set_expire;
95 0           $self;
96             }
97              
98             sub get_value {
99 0     0 1   my ( $self, $key ) = @_;
100 0           my $value = $self->{cmf}->get($key);
101 0 0         return unless defined $value;
102 0           decode_cbor $value;
103             }
104              
105             sub set_value {
106 0     0 1   my ( $self, $key, $value ) = @_;
107 0           $self->{cmf}->set( $key => encode_cbor $value);
108             }
109              
110             sub destroy {
111 0     0 1   my ($self) = @_;
112 0           $self->{cmf}->flush_all;
113 0           undef;
114             }
115              
116             sub flush {
117 0     0 1   my $self = shift;
118 0           $self->{cmf}->nowait_push;
119 0           $self;
120             }
121              
122             1;
123              
124             __END__