ASP/SessionStateManager/Memcached.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 105 CC 5 SCAR 15.5
CC
5
Cov
0%
new
12.0
retrieve
6.0
save
6.0
CRAP 4.7
1  
2 package Apache2::ASP::SessionStateManager::Memcached;
3  
4 use strict;
5 use warnings 'all';
6 use base 'Apache2::ASP::SessionStateManager';
7 use Cache::Memcached;
8 use Digest::MD5 'md5_hex';
9  
10  
11 #==============================================================================
12 sub new
13 {
14 my ($class, %args) = @_;
15
16 my $s = bless { }, $class;
17
18 # Prepare our Session:
19 if( my $id = $s->parse_session_id() )
20 {
21 if( $s->verify_session_id( $id ) )
22 {
23 $s->{SessionID} = $id;
24 return $s->retrieve( $id );
25 }
26 else
27 {
28 $s->{SessionID} = $s->new_session_id();
29 $s->write_session_cookie();
30 return $s->create( $s->{SessionID} );
31 }# end if()
32 }
33 else
34 {
35 $s->{SessionID} = $s->new_session_id();
36 $s->write_session_cookie();
37 return $s->create( $s->{SessionID} );
38 }# end if()
39 }# end new()
40  
41  
42 #==============================================================================
43 sub memd
44 {
45 my $s = shift;
46  
47 $s->{memd} ||= new Cache::Memcached {
48 'servers' => [
49 split(/,\s*/, $s->context->config->data_connections->session->dsn )
50 ]
51 };
52 $s->{memd};
53 }# end memd()
54  
55  
56 #==============================================================================
57 sub verify_session_id
58 {
59 my ($s, $SessionID) = @_;
60
61 defined( $s->memd->get( $SessionID ) );
62 }# end verify_session_id()
63  
64  
65 #==============================================================================
66 sub retrieve
67 {
68 my ($s, $SessionID) = @_;
69
70 my $got = $s->memd->get( $SessionID );
71 $got->{memd} = $s->memd;
72 $got = bless $got, ref($s) || $s
73 unless UNIVERSAL::isa( $got, __PACKAGE__ );
74 return $got;
75 }# end retrieve()
76  
77  
78 #==============================================================================
79 sub create
80 {
81 my ($s, $SessionID) = @_;
82
83 eval {
84 no warnings 'uninitialized';
85 $s->{__signature} = md5_hex(
86 join ":",
87 map { "$_:$s->{$_}" }
88 grep { $_ && $_ !~ m/^(memd|__signature)/ } sort keys(%$s)
89 );
90 };
91
92 $s->memd->set(
93 $SessionID => $s,
94 $s->context->config->data_connections->session->session_timeout * 60
95 );
96 $s;
97 }# end create()
98  
99  
100 #==============================================================================
101 sub save
102 {
103 my ($s) = @_;
104
105 no warnings 'uninitialized';
106 return if eval { $s->{__signature} eq md5_hex(
107 join ":", map { "$_:$s->{$_}" }
108 grep { $_ && $_ !~ m/^(memd|__signature)/ } sort keys(%$s)
109 )};
110 eval {
111 $s->{__signature} = md5_hex(
112 join ":",
113 map { "$_:$s->{$_}" }
114 grep { $_ && $_ !~ m/^(memd|__signature)/ } sort keys(%$s)
115 );
116 };
117
118 $s->memd->set(
119 $s->{SessionID} => $s,
120 $s->context->config->data_connections->session->session_timeout * 60
121 );
122 1;
123 }# end save()
124  
125  
126 1;# return true:
127