MtPolicyd/SessionCache/Memcached.pm

filter: Statementstmt 29.7% 11 / 37 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 20.0% 1 / 5 Podpod 0.0% 0 / 2 totaltotal 20.3% 12 / 59 CC 7 SCAR 23.1
CC
7
Cov
22%
_acquire_session_lock
12.0
retrieve_session
12.0
store_session
12.0
CRAP 10.1
« MtPolicyd/Client/App.pm ↑ Summary MtPolicyd/Plugin/RBLAction.pm »
1 package Mail::MtPolicyd::SessionCache::Memcached;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '2.01'; # VERSION
6 # ABSTRACT: session cache adapter for memcached
7  
8 1 extends 'Mail::MtPolicyd::SessionCache::Base';
9  
10 1 with 'Mail::MtPolicyd::Role::Connection' => {
11 name => 'memcached',
12 type => 'Memcached',
13 };
14  
15  
16 1 has 'expire' => ( is => 'ro', isa => 'Int', default => 5 * 60 );
17  
18 1 has 'lock_wait' => ( is => 'rw', isa => 'Int', default => 50 );
19 1 has 'lock_max_retry' => ( is => 'rw', isa => 'Int', default => 50 );
20 1 has 'lock_timeout' => ( is => 'rw', isa => 'Int', default => 10 );
21  
22 sub _acquire_session_lock {
23 0 ▶ my ( $self, $instance ) = @_;
Subroutine _acquire_session_lock: not called CC=3 SCAR=24.8
24 0 my $lock = 'lock_'.$instance;
25  
26 0 ▶ for( my $try = 1 ; $try < $self->lock_max_retry ; $try++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try < $self->lock_max_retry) 0 0 0
27 0 ▶ if( $self->_memcached_handle->add($lock, 1, $self->lock_timeout) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_memcached_handle->add($lock, 1, $self->lock_timeout)) 0 0 0
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 ▶ my ( $self, $instance ) = @_;
Subroutine _release_session_lock: not called CC=1 SCAR=6.9
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 ▶ my ($self, $instance ) = @_;
Subroutine retrieve_session: not called CC=3 SCAR=24.8
Pod undocumented
48  
49 0 ▶ if( ! defined $instance ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $instance) 0 0 0
50 0 return;
51 }
52  
53 0 $self->_acquire_session_lock( $instance );
54  
55 0 ▶ if( my $session = $self->_memcached_handle->get($instance) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $session = $self->_memcached_handle->get($instance)) 0 0 0
56 0 return($session);
57 }
58
59 0 return( { '_instance' => $instance } );
60 }
61  
62 sub store_session {
63 0 ▶ my ($self, $session ) = @_;
Subroutine store_session: not called CC=3 SCAR=24.8
Pod undocumented
64 0 my $instance = $session->{'_instance'};
65  
66 0 ▶ if( ! defined $session || ! defined $instance ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $session and defined $instance) 0 0 0
Condition0%0/3
defined $session and defined $instance
!ll&&!rl&&r
000
MC/DC0%0/2
defined $session and defined $instance
defined $session defined $instance
Truth table0%0/3
A and B
A= defined $sessionB= defined $instance
ABresult
0X0
100
111
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 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
« MtPolicyd/Client/App.pm ↑ Summary MtPolicyd/Plugin/RBLAction.pm »