File Coverage

blib/lib/Dancer/Session/Memcached/Fast.pm
Criterion Covered Total %
statement 24 73 32.8
branch 0 10 0.0
condition 0 9 0.0
subroutine 8 22 36.3
pod 8 8 100.0
total 40 122 32.7


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