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