File Coverage

blib/lib/Dancer2/Session/Memcached.pm
Criterion Covered Total %
statement 23 24 95.8
branch n/a
condition n/a
subroutine 8 9 88.8
pod n/a
total 31 33 93.9


line stmt bran cond sub pod time code
1 2     2   14730 use strict;
  2         3  
  2         61  
2 2     2   10 use warnings;
  2         3  
  2         125  
3              
4             package Dancer2::Session::Memcached;
5             our $AUTHORITY = 'cpan:YANICK';
6             # ABSTRACT: Dancer 2 session storage with Cache::Memcached
7             $Dancer2::Session::Memcached::VERSION = '0.005';
8 2     2   8 use Moo;
  2         3  
  2         18  
9 2     2   1113 use Cache::Memcached;
  2         63388  
  2         85  
10              
11 2     2   1220 use Types::Standard qw/ Str ArrayRef InstanceOf /;
  2         133282  
  2         22  
12              
13 2     2   1671 use Type::Tiny;
  2         2  
  2         624  
14              
15             my $Server = Type::Tiny->new(
16             name => 'MemcachedServer',
17             parent => Str,
18             constraint => sub { ! /^\d+\.\d+\.\d+\.\d+$/ },
19             message => sub {
20             "server `$_' is invalid; port is missing, use `server:port'"
21             },
22              
23             );
24              
25             my $Servers = Type::Tiny->new(
26             name => 'MemcachedServers',
27             parent => ArrayRef[$Server],
28             coercion => Type::Coercion->new( type_coercion_map => [
29             Str ,=> sub { [ split ',', $_ ] },
30             ]),
31             );
32              
33              
34             #--------------------------------------------------------------------------#
35             # Public attributes
36             #--------------------------------------------------------------------------#
37              
38              
39             has memcached_servers => (
40             is => 'ro',
41             isa => $Servers,
42             required => 1,
43             coerce => $Servers->coercion,
44             );
45              
46             #--------------------------------------------------------------------------#
47             # Private attributes
48             #--------------------------------------------------------------------------#
49              
50             has _memcached => (
51             is => 'lazy',
52             isa => InstanceOf ['Cache::Memcached'],
53             handles => {
54             _retrieve => 'get',
55             _flush => 'set',
56             _destroy => 'delete',
57             },
58             );
59              
60             # Adapted from Dancer::Session::Memcached
61             sub _build__memcached {
62 1     1   101166 my ($self) = @_;
63 1         21 return Cache::Memcached->new( servers => $self->memcached_servers );
64             }
65              
66             #--------------------------------------------------------------------------#
67             # Role composition
68             #--------------------------------------------------------------------------#
69              
70             with 'Dancer2::Core::Role::SessionFactory';
71              
72             # _retrieve, _flush, _destroy handled by _memcached object
73              
74             # memcached doesn't have any easy way to list keys it knows about
75             # so we cheat and return an empty array ref
76 0     0   0 sub _sessions { [] }
77              
78             sub _change_id {
79 1     1   48443 my ( $self, $old_id, $new_id ) = @_;
80 1         19 $self->_flush( $new_id, $self->_retrieve( $old_id ) );
81 1         65 $self->_destroy( $old_id );
82             }
83              
84             1;
85              
86              
87             # vim: ts=4 sts=4 sw=4 et:
88              
89             __END__