line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
10028
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
59
|
|
2
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
113
|
|
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.007'; |
8
|
2
|
|
|
2
|
|
11
|
use Moo; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
13
|
|
9
|
2
|
|
|
2
|
|
1145
|
use Cache::Memcached; |
|
2
|
|
|
|
|
67036
|
|
|
2
|
|
|
|
|
75
|
|
10
|
2
|
|
|
2
|
|
13
|
use Carp qw/ croak /; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
134
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
14
|
use Types::Standard qw/ Str ArrayRef InstanceOf /; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
39
|
|
13
|
|
|
|
|
|
|
|
14
|
2
|
|
|
2
|
|
2107
|
use Type::Tiny; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
823
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $Server = Type::Tiny->new( |
17
|
|
|
|
|
|
|
name => 'MemcachedServer', |
18
|
|
|
|
|
|
|
parent => Str, |
19
|
|
|
|
|
|
|
constraint => sub { ! /^\d+\.\d+\.\d+\.\d+$/ }, |
20
|
|
|
|
|
|
|
message => sub { |
21
|
|
|
|
|
|
|
"server `$_' is invalid; port is missing, use `server:port'" |
22
|
|
|
|
|
|
|
}, |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my $Servers = Type::Tiny->new( |
27
|
|
|
|
|
|
|
name => 'MemcachedServers', |
28
|
|
|
|
|
|
|
parent => ArrayRef[$Server], |
29
|
|
|
|
|
|
|
coercion => Type::Coercion->new( type_coercion_map => [ |
30
|
|
|
|
|
|
|
Str ,=> sub { [ split ',', $_ ] }, |
31
|
|
|
|
|
|
|
]), |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
36
|
|
|
|
|
|
|
# Public attributes |
37
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
has memcached_servers => ( |
41
|
|
|
|
|
|
|
is => 'ro', |
42
|
|
|
|
|
|
|
isa => $Servers, |
43
|
|
|
|
|
|
|
required => 1, |
44
|
|
|
|
|
|
|
coerce => $Servers->coercion, |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has fatal_cluster_unreachable => ( |
48
|
|
|
|
|
|
|
is => 'ro', |
49
|
|
|
|
|
|
|
required => 0, |
50
|
|
|
|
|
|
|
default => sub { 0 }, |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
54
|
|
|
|
|
|
|
# Private attributes |
55
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
has _memcached => ( |
58
|
|
|
|
|
|
|
is => 'lazy', |
59
|
|
|
|
|
|
|
isa => InstanceOf ['Cache::Memcached'], |
60
|
|
|
|
|
|
|
handles => { |
61
|
|
|
|
|
|
|
_destroy => 'delete', |
62
|
|
|
|
|
|
|
}, |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub _retrieve { |
66
|
7
|
|
|
7
|
|
30
|
my ($self) = shift; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
croak "Memcache cluster unreachable _retrieve" |
69
|
7
|
50
|
33
|
|
|
37
|
if $self->fatal_cluster_unreachable && not keys %{$self->_memcached->stats(['misc'])}; |
|
0
|
|
|
|
|
0
|
|
70
|
|
|
|
|
|
|
|
71
|
7
|
|
|
|
|
100
|
return $self->_memcached->get( @_ ); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _flush { |
75
|
7
|
|
|
7
|
|
146508
|
my ($self) = shift; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
croak "Memcache cluster unreachable _flush" |
78
|
7
|
50
|
33
|
|
|
30
|
if $self->fatal_cluster_unreachable && not keys %{$self->_memcached->stats(['misc'])}; |
|
0
|
|
|
|
|
0
|
|
79
|
|
|
|
|
|
|
|
80
|
7
|
|
|
|
|
98
|
return $self->_memcached->set( @_ ); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# Adapted from Dancer::Session::Memcached |
84
|
|
|
|
|
|
|
sub _build__memcached { |
85
|
1
|
|
|
1
|
|
12
|
my ($self) = @_; |
86
|
|
|
|
|
|
|
|
87
|
1
|
|
|
|
|
27
|
return Cache::Memcached->new( servers => $self->memcached_servers ); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
91
|
|
|
|
|
|
|
# Role composition |
92
|
|
|
|
|
|
|
#--------------------------------------------------------------------------# |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
with 'Dancer2::Core::Role::SessionFactory'; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# _retrieve, _flush, _destroy handled by _memcached object |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# memcached doesn't have any easy way to list keys it knows about |
99
|
|
|
|
|
|
|
# so we cheat and return an empty array ref |
100
|
0
|
|
|
0
|
|
0
|
sub _sessions { [] } |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub _change_id { |
103
|
1
|
|
|
1
|
|
396
|
my ( $self, $old_id, $new_id ) = @_; |
104
|
1
|
|
|
|
|
4
|
$self->_flush( $new_id, $self->_retrieve( $old_id ) ); |
105
|
1
|
|
|
|
|
27
|
$self->_destroy( $old_id ); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# reject anything where the first two bytes are below \x20 once |
109
|
|
|
|
|
|
|
# Base64 decoded, ensuring Storable doesnt attempt to thaw such cruft. |
110
|
|
|
|
|
|
|
sub validate_id { |
111
|
6
|
|
|
6
|
0
|
68364
|
$_[1] =~ m/^[I-Za-z0-9_\-~][A-Za-z0-9_\-~]+$/; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
1; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# vim: ts=4 sts=4 sw=4 et: |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
__END__ |