Connector/Role/SessionCache.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 102 CC 8 SCAR 26.3
CC
8
Cov
0%
detach_session
30.0
attach_session
12.0
register_session
6.0
CRAP 13.9
1 package Connector::Role::SessionCache;
2  
3 use strict;
4 use warnings;
5 use English;
6  
7 use Moose::Role;
8  
9 our %sessioncache;
10  
11 requires 'authid';
12 requires 'terminate_session';
13 requires 'validate_session';
14  
15 has session => (
16 is => 'rw',
17 isa => 'Str',
18 required => 0,
19 lazy => 1,
20 builder => '_init_session',
21 clearer => 'clear_session',
22 predicate => 'has_session',
23 );
24  
25 sub DEMOLISH {};
26  
27 before 'DEMOLISH' => sub {
28  
29 my $self = shift;
30 # do not cleanup when program is already dying
31 # as the linked objects might no longer be there we will get
32 # unpredicatable behaviour and ugly errors in the logs
33 if (${^GLOBAL_PHASE} ne 'DESTRUCT') {
34 $self->detach_session();
35 }
36 };
37  
38 =head2 attach_session
39  
40 Reads authid from the class and checks if the session cache has a
41 suitable item. If found, the session is validated, the reference
42 counter is increased and the session id is returned. If a session
43 fails validation, it is removed from the cache. If no valid session
44 is found, returns undef.
45  
46 =cut
47  
48 sub attach_session {
49  
50 my $self = shift;
51 my $authid = $self->authid();
52 return unless ($sessioncache{$authid});
53  
54 my ($instcnt, $session) = @{$sessioncache{$authid}};
55 if ($session && !$self->validate_session($session)) {
56 delete $sessioncache{$authid};
57 $self->clear_session();
58 $self->log()->debug('Cached session already gone - discard');
59 } else {
60 $self->log()->debug('Allocate session id from cache for ' . $authid );
61 $sessioncache{$authid}->[0]++;
62 return $session;
63 }
64 return;
65 }
66  
67 =head2 register_session
68  
69 Expects the session information to store in the session cache as
70 argument and puts it into the sessioncache object using the class I<authid>
71 from the class as index.
72  
73 Existing items in the cache will be overwritten, passing I<undef>
74 removes the cache item.
75  
76 Returns the session information.
77  
78 =cut
79  
80 sub register_session {
81  
82 my $self = shift;
83 my $session = shift;
84 my $authid = $self->authid();
85 if ($session) {
86 $sessioncache{$authid} = [ 1, $session ];
87 } else {
88 $sessioncache{$authid} = undef;
89 }
90 return $session;
91  
92 }
93  
94 =head2 detach_session
95  
96 Detach this instance from the session cache and terminate the session
97 in case this is the last reference to it. The session can be passed as
98 argument, in case no argument or undef is passed, the session is read
99 from the class I<session> method.
100  
101 =cut
102  
103 sub detach_session {
104  
105 my $self = shift;
106 my $session = shift;
107  
108 return unless ($session || $self->has_session());
109 $session ||= $self->session();
110  
111 $self->log()->debug('Detach session ' . $session);
112 my $authid = $self->authid();
113 return unless ($sessioncache{$authid});
114  
115 # using cmp allows us to overload the comparission when using objects
116 # NB: cmp = 0 if strings are equal
117 return if ($sessioncache{$authid}->[1] cmp $session);
118  
119 if ($sessioncache{$authid}->[0] > 1) {
120 $sessioncache{$authid}->[0]--;
121 $self->log()->debug(sprintf('Session has %01d other references', $sessioncache{$authid}->[0]));
122 return;
123 }
124  
125 $self->log()->trace('Last reference to session - terminating');
126 delete $sessioncache{$authid};
127 $self->terminate_session( $session );
128 return 1;
129  
130 }
131  
132 1;
133  
134 __END__;