line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mail::MtPolicyd::SessionCache::Memcached; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1244
|
use Moose; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
6
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '2.02'; # VERSION |
6
|
|
|
|
|
|
|
# ABSTRACT: session cache adapter for memcached |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
extends 'Mail::MtPolicyd::SessionCache::Base'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
with 'Mail::MtPolicyd::Role::Connection' => { |
11
|
|
|
|
|
|
|
name => 'memcached', |
12
|
|
|
|
|
|
|
type => 'Memcached', |
13
|
|
|
|
|
|
|
}; |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
4948
|
use Time::HiRes qw(usleep); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
10
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has 'expire' => ( is => 'ro', isa => 'Int', default => 5 * 60 ); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has 'lock_wait' => ( is => 'rw', isa => 'Int', default => 50 ); |
21
|
|
|
|
|
|
|
has 'lock_max_retry' => ( is => 'rw', isa => 'Int', default => 50 ); |
22
|
|
|
|
|
|
|
has 'lock_timeout' => ( is => 'rw', isa => 'Int', default => 10 ); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub _acquire_session_lock { |
25
|
0
|
|
|
0
|
|
|
my ( $self, $instance ) = @_; |
26
|
0
|
|
|
|
|
|
my $lock = 'lock_'.$instance; |
27
|
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
for( my $try = 1 ; $try < $self->lock_max_retry ; $try++ ) { |
29
|
0
|
0
|
|
|
|
|
if( $self->_memcached_handle->add($lock, 1, $self->lock_timeout) ) { |
30
|
0
|
|
|
|
|
|
return; # lock created |
31
|
|
|
|
|
|
|
} |
32
|
0
|
|
|
|
|
|
usleep( $self->lock_wait * $try ); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
die('could not acquire lock for session '.$instance); |
36
|
0
|
|
|
|
|
|
return; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub _release_session_lock { |
40
|
0
|
|
|
0
|
|
|
my ( $self, $instance ) = @_; |
41
|
0
|
|
|
|
|
|
my $lock = 'lock_'.$instance; |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
$self->_memcached_handle->delete($lock); |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
return; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub retrieve_session { |
49
|
0
|
|
|
0
|
0
|
|
my ($self, $instance ) = @_; |
50
|
|
|
|
|
|
|
|
51
|
0
|
0
|
|
|
|
|
if( ! defined $instance ) { |
52
|
0
|
|
|
|
|
|
return; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
$self->_acquire_session_lock( $instance ); |
56
|
|
|
|
|
|
|
|
57
|
0
|
0
|
|
|
|
|
if( my $session = $self->_memcached_handle->get($instance) ) { |
58
|
0
|
|
|
|
|
|
return($session); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
return( { '_instance' => $instance } ); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub store_session { |
65
|
0
|
|
|
0
|
0
|
|
my ($self, $session ) = @_; |
66
|
0
|
|
|
|
|
|
my $instance = $session->{'_instance'}; |
67
|
|
|
|
|
|
|
|
68
|
0
|
0
|
0
|
|
|
|
if( ! defined $session || ! defined $instance ) { |
69
|
0
|
|
|
|
|
|
return; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
$self->_memcached_handle->set($instance, $session, $self->expire); |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
$self->_release_session_lock($instance); |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
return; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
1; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
__END__ |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=pod |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=encoding UTF-8 |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 NAME |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Mail::MtPolicyd::SessionCache::Memcached - session cache adapter for memcached |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 VERSION |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
version 2.02 |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head1 SYNOPSIS |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
<SessionCache> |
98
|
|
|
|
|
|
|
module = "Memcached" |
99
|
|
|
|
|
|
|
#memcached = "memcached" |
100
|
|
|
|
|
|
|
# expire session cache entries |
101
|
|
|
|
|
|
|
expire = "300" |
102
|
|
|
|
|
|
|
# wait timeout will be increased each time 50,100,150,... (usec) |
103
|
|
|
|
|
|
|
lock_wait=50 |
104
|
|
|
|
|
|
|
# abort after n retries |
105
|
|
|
|
|
|
|
lock_max_retry=50 |
106
|
|
|
|
|
|
|
# session lock times out after (sec) |
107
|
|
|
|
|
|
|
lock_timeout=10 |
108
|
|
|
|
|
|
|
</SessionCache> |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 PARAMETERS |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=over |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=item memcached (default: memcached) |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Name of the database connection to use. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
You have to define this connection first. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
see L<Mail::MtPolicyd::Connection::Memcached> |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item expire (default: 5*60) |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Timeout in seconds for sessions. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=item lock_wait (default: 50) |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Timeout for retry when session is locked in milliseconds. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
The retry will be done in multiples of this timeout. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
When set to 50 retry will be done in 50, 100, 150ms... |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item lock_max_retry (default: 50) |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Maximum number of retries before giving up to obtain lock on a |
137
|
|
|
|
|
|
|
session. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=item lock_timeout (default: 10) |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Timeout of session locks in seconds. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=back |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 AUTHOR |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Markus Benning <ich@markusbenning.de> |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
This is free software, licensed under: |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
The GNU General Public License, Version 2, June 1991 |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=cut |