Wombat/SessionManager/StandardSessionManager.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 99 CC 6 SCAR 15.5
CC
6
Cov
0%
start
12.0
new
6.0
getSessions
6.0
CRAP 4.7
1 # -*- Mode: Perl; indent-tabs-mode: nil; -*-
2  
3 package Wombat::SessionManager::StandardSessionManager;
4  
5 =pod
6  
7 =head1 NAME
8  
9 Wombat::SessionManager::StandardSessionManager - caching session
10 manager implementation
11  
12 =head1 SYNOPSIS
13  
14 =head1 DESCRIPTION
15  
16 Subclass of B<Wombat::SessionManager::SessionManagerBase> that uses an
17 implementation of B<Cache::Cache> to manage a lightweight cache of
18 sessions. Typically B<Cache::MemoryCache> will be used in
19 single-process scenarios; B<Cache::SharedMemoryCache> is useful for
20 multi-process deployments. This class does not support any persistence
21 or distributable capabilities.
22  
23 =cut
24  
25 use base qw(Wombat::SessionManager::SessionManagerBase);
26 use fields qw(cache cacheClass);
27 use strict;
28 use warnings;
29  
30 use Wombat::Exception ();
31  
32 =pod
33  
34 =head1 CONSTRUCTOR
35  
36 =over
37  
38 =item new()
39  
40 Construct and return a B<Wombat::SessionManager::StandardSessionManager>
41 instance, initializing fields appropriately. If subclasses override
42 the constructor, they must be sure to call
43  
44 $self->SUPER::new();
45  
46 =back
47  
48 =cut
49  
50 sub new {
51 my $self = shift;
52  
53 $self = fields::new($self) unless ref $self;
54 $self->SUPER::new();
55  
56 $self->{cacheClass} = {};
57 $self->{cache} = undef;
58  
59 return $self;
60 }
61  
62 =pod
63  
64 =head1 ACCESSOR METHODS
65  
66 =over
67  
68 =item getCacheClass()
69  
70 Return the cache class for this SessionManager.
71  
72 =cut
73  
74 sub getCacheClass {
75 my $self = shift;
76  
77 return $self->{cacheClass};
78 }
79  
80 =pod
81  
82 =item setCacheClass($class)
83  
84 Set the cache class for this SessionManager.
85  
86 B<Parameters:>
87  
88 =over
89  
90 =item $class
91  
92 the B<Cache::Cache> implementation to use for the cache
93  
94 =back
95  
96 =cut
97  
98 sub setCacheClass {
99 my $self = shift;
100 my $class = shift;
101  
102 $self->{cacheClass} = $class;
103  
104 return 1;
105 }
106  
107 =back
108  
109 =head1 PUBLIC METHODS
110  
111 =over
112  
113 =back
114  
115 =head1 PACKAGE METHODS
116  
117 =over
118  
119 =item add($session)
120  
121 Add this Session to the cache of active Sessions for this
122 SessionManager.
123  
124 B<Parameters:>
125  
126 =over
127  
128 =item $session
129  
130 the B<Wombat::Session> to be added
131  
132 =back
133  
134 =cut
135  
136 sub add {
137 my $self = shift;
138 my $session = shift;
139  
140 # don't use Cache::Cache's expiration mechanism since we have our
141 # own expiration model. this means that if a Session is not found
142 # in the cache, it never existed or has previously been explicitly
143 # removed.
144  
145 $self->{cache}->set($session->getId(), $session);
146  
147 return 1;
148 }
149  
150 =pod
151  
152 =item getName()
153  
154 Return the display name of this SessionManager.
155  
156 =cut
157  
158 sub getName {
159 return "StandardSessionManager";
160 }
161  
162 =pod
163  
164 =item getSession($id)
165  
166 Return the active Session cached by this SessionManager with the
167 specified id, or C<undef> if no session exists with that id.
168  
169 B<Parameters:>
170  
171 =over
172  
173 =item $id
174  
175 the id for the Session to be returned
176  
177 =back
178  
179 =cut
180  
181 sub getSession {
182 my $self = shift;
183 my $id = shift;
184  
185 return $self->{cache}->get($id);
186 }
187  
188 =pod
189  
190 =item getSessions()
191  
192 Return an array containing the active Sessions cached by this
193 SessionManager.
194  
195 =cut
196  
197 sub getSessions {
198 my $self = shift;
199  
200 warn "ids: ", join('|', $self->{cache}->get_identifiers()), "\n";
201  
202 my @sessions = map { $self->{cache}->get($_) }
203 $self->{cache}->get_identifiers();
204  
205 return wantarray ? @sessions : \@sessions;
206 }
207  
208 =pod
209  
210 =item remove($session)
211  
212 Remove this Session from this SessionManager's cache.
213  
214 B<Parameters:>
215  
216 =over
217  
218 =item $session
219  
220 the B<Wombat::Session> to remove
221  
222 =back
223  
224 =cut
225  
226 sub remove {
227 my $self = shift;
228 my $session = shift;
229  
230 $self->{cache}->remove($session->getId()) if $session;
231  
232 return 1;
233 }
234  
235 =pod
236  
237 =item save($session)
238  
239 Write the current state of this Session to this SessionManager's
240 cache.
241  
242 B<Parameters:>
243  
244 =over
245  
246 =item $session
247  
248 the B<Wombat::Session> to save
249  
250 =back
251  
252 =cut
253  
254 sub save {
255 my $self = shift;
256 my $session = shift;
257  
258 $self->{cache}->set($session->getId(), $session);
259  
260 return 1;
261 }
262  
263 =pod
264  
265 =back
266  
267 =head1 LIFECYCLE METHODS
268  
269 =over
270  
271 =item start()
272  
273 Prepare for active use of this SessionManager and initialize the
274 session cache. This method should be called before any of the public
275 methods of the SessionManager are utilized.
276  
277 B<Throws:>
278  
279 =over
280  
281 =item B<Wombat::LifecycleException>
282  
283 if the SessionManager has already been started or if the cache cannot
284 be initialized
285  
286 =back
287  
288 =cut
289  
290 sub start {
291 my $self = shift;
292  
293 $self->SUPER::start();
294  
295 undef $self->{started};
296  
297 my $class = $self->{cacheClass};
298  
299 # load cache class
300 eval "require $class";
301 if ($@) {
302 my $msg = "setCacheClass: class load problem: $@";
303 Wombat::LifecycleException->throw($msg);
304 }
305  
306 # ensure the cache class implements Cache::Cache
307 unless (UNIVERSAL::isa($class, 'Cache::Cache')) {
308 my $msg = "setCacheClass: class [$class] does not extend Cache::Cache";
309 Wombat::LifecycleException->throw($msg);
310 }
311  
312 $self->{cache} = $class->new({namespace => 'Wombat'});
313  
314 $self->{started} = 1;
315  
316 return 1;
317 }
318  
319 1;
320 __END__