File Coverage

blib/lib/Apache/Session/Store/Memorycached.pm
Criterion Covered Total %
statement 74 121 61.1
branch 12 44 27.2
condition 5 12 41.6
subroutine 12 13 92.3
pod 0 6 0.0
total 103 196 52.5


line stmt bran cond sub pod time code
1             ############################################################################
2             #
3             # Apache::Session::Store::Memorycached
4             # Implements session object storage via memcached
5             # Copyright(c) eric german
6             # Distribute under the Artistic License
7             #
8             ############################################################################
9              
10             package Apache::Session::Store::Memorycached;
11              
12 2     2   10 use strict;
  2         2  
  2         65  
13 2     2   16196 use Symbol;
  2         2151  
  2         186  
14 2     2   4829 use Data::Dumper;
  2         28230  
  2         569  
15 2     2   2030 use Cache::Memcached;
  2         347236  
  2         97  
16 2     2   24 use Digest::MD5 qw(md5_hex);
  2         5  
  2         1327  
17 2     2   39 use vars qw($VERSION);
  2         4  
  2         2645  
18             $VERSION = '2.2';
19              
20              
21             sub new {
22             #This constructor allocate memory space for for the package!!
23 2     2 0 6 my $class = shift;
24 2         4 my $self;
25 2         7 $self->{opened} = 0;
26            
27 2         15 return bless $self, $class;
28             }
29              
30              
31             sub insert {
32             #This function is called, when a tie instruction is launch with an undef id
33             #Otherwise at the first identification ( cf Login.pm )
34 1     1 0 6 my $self = shift;
35 1         3 my $session = shift;
36              
37 1 50 33     9 if (! exists $session->{args}->{updateOnly}
38             || $session->{args}->{updateOnly} != 1 ) {
39              
40 1         2 my $retour;
41 1         4 my $ryserver = $session->{args}->{servers};
42 1         4 my $ryserverlocal = $session->{args}->{local};
43 1   50     10 my $rytimeout = $session->{args}->{timeout}||'0';
44 1         13 my $memd= new Cache::Memcached { 'servers' => $ryserver };
45 1         4225 my $ident = $session->{data}->{_session_id};
46 1         3 my $rhash = $session->{data};
47 1         7 $retour = $memd->set($ident,$rhash,$rytimeout);
48 1 50       1591 if($retour!=1){
49 1         9 $memd->set($ident,$rhash,$rytimeout);
50             }
51 1 50       61 if ($ryserverlocal)
52             {
53 0         0 my $memdlocal= new Cache::Memcached { 'servers' => $ryserverlocal};
54 0         0 my $identlocal = $session->{data}->{_session_id};
55 0         0 my $rhashlocal = $session->{data};
56 0         0 $retour = $memdlocal->set($identlocal,$rhashlocal,$rytimeout);
57 0 0       0 if($retour!=1){
58 0         0 $memdlocal->set($identlocal,$rhashlocal,$rytimeout);
59             }
60            
61            
62             }
63             }
64            
65 1         11 $self->{opened} = 1;
66            
67             }
68              
69             sub update {
70 2     2 0 14 my $self = shift;
71 2         3 my $session = shift;
72 2         3 my $retour;
73 2         5 my $ryserver = $session->{args}->{servers};
74 2         4 my $ryserverlocal = $session->{args}->{local};
75 2   50     14 my $rytimeout = $session->{args}->{timeout}||'0';
76 2         4 my $principalkey;
77             my $keyvalue;
78 2         14 my $memd= new Cache::Memcached { 'servers' => $ryserver };
79            
80 2         428 my $ident = $session->{data}->{_session_id} ;
81 2         4 my $rhash = $session->{data};
82 2 50       9 if ( $session->{args}->{principal} ) {
83 0         0 $principalkey = $session->{args}->{principal} ;
84 0         0 $keyvalue= $session->{data}->{$principalkey} ;
85 0         0 $keyvalue = md5_hex($keyvalue) ;
86 0 0       0 $memd->set($keyvalue,$ident,$rytimeout) if $keyvalue;
87 0         0 my $identp = $principalkey.'_MD5';
88 0         0 $session->{data}->{$identp} = $keyvalue ;
89             }
90 2         7 $retour = $memd->set($ident,$rhash,$rytimeout);
91              
92              
93              
94 2 50       65 if($retour!=1){
95              
96              
97             }
98              
99 2 50       8 if ($ryserverlocal)
100             {
101 0         0 my $memdlocal= new Cache::Memcached { 'servers' => $ryserverlocal};
102 0         0 my $identlocal = $session->{data}->{_session_id};
103 0         0 my $rhashlocal = $session->{data};
104             #### in order to prepare identify federation ####
105 0 0       0 if ( $session->{args}->{principal} ) {
106 0 0       0 $memdlocal->set($keyvalue,$identlocal,$rytimeout) if $keyvalue;
107             }
108              
109 0         0 $retour = $memdlocal->set($identlocal,$rhashlocal,$rytimeout);
110              
111              
112              
113 0 0       0 if($retour!=1){
114              
115              
116             }
117              
118              
119             }
120             ##################################################
121 2         17 $self->{opened} = 1;
122             }
123              
124             sub materialize {
125 1     1 0 7 my $self = shift;
126 1         3 my $session = shift;
127              
128 1         3 my $ryserver = $session->{args}->{servers};
129 1         3 my $rhash;
130 1         3 my $ryserverlocal = $session->{args}->{local};
131 1   50     9 my $rytimeout = $session->{args}->{timeout}||'0';
132 1 50       4 if ($ryserverlocal)
133             {
134 0         0 my $memdlocal= new Cache::Memcached { 'servers' => $ryserverlocal};
135 0         0 my $identlocal = $session->{data}->{_session_id};
136 0         0 $rhash = $memdlocal->get($identlocal);
137             }
138             #####
139             ####
140             ####
141 1         2 my @tabkey = keys (%{$rhash}) ;
  1         5  
142 1 50       9 if ($#tabkey <1)
143             {
144             ### not found in local cache , I retrieve session on primary server
145             #print STDERR "MATERIALIZE : RIEN SUR SERVEUR LOCAL $$ !!!\n";
146 1         7 my $memd= new Cache::Memcached { 'servers' => $ryserver };
147 1         198 my $ident = $session->{data}->{_session_id};
148 1         13 $rhash = $memd->get($ident);
149 1 50       54 if(!defined($rhash)){
150            
151              
152             }
153             ## the data is in the principal cache notin the local cache
154             ## we must put data in it.
155 1 50 33     9 if ($ryserverlocal && $rhash)
156             {
157             #print STDERR "MATERIALIZE : REPERCUSSION SUR SERVEUR LOCAL $$ !!!\n";
158 0         0 my $memdlocal= new Cache::Memcached { 'servers' => $ryserverlocal};
159 0         0 my $identlocal = $session->{data}->{_session_id};
160             #### oups ! ....bug corrected
161             # my $rhashlocal = $session->{data};mistake
162 0         0 my $rhashlocal = $rhash;
163 0         0 $memdlocal->set($identlocal,$rhash,$rytimeout);
164 0 0       0 if($!){
165              
166            
167             }
168             }
169              
170             }
171              
172 1         4 $self->{opened} = 1;
173            
174 1         4 $session->{data} =$rhash;
175             #if(!defined($rhash)){
176             #$session->{error} = 1;
177             # }
178            
179             }
180              
181             sub remove {
182 0     0 0 0 my $self = shift;
183 0         0 my $session = shift;
184              
185 0         0 my $ryserver = $session->{args}->{servers};
186              
187              
188 0         0 my $memd= new Cache::Memcached { 'servers' => $ryserver};
189 0         0 my $principalkey;
190             my $identp;
191 0         0 my $keyvalue;
192 0         0 my $ryserverlocal = $session->{args}->{local};
193 0         0 my $ident = $session->{data}->{_session_id} ;
194 0 0       0 if ( $session->{args}->{principal} ) {
195              
196 0         0 $principalkey = $session->{args}->{principal} ;
197 0         0 $identp = $principalkey.'_MD5';
198 0         0 $keyvalue= $session->{data}->{$identp} ;
199 0 0       0 $memd->delete($keyvalue) if $keyvalue ;
200             }
201 0         0 $memd->delete($ident);
202 0 0       0 if ($ryserverlocal)
203             {
204 0         0 my $memdlocal= new Cache::Memcached { 'servers' => $ryserverlocal };
205 0         0 my $identlocal = $session->{data}->{_session_id};
206 0 0       0 $memdlocal->delete($keyvalue) if $keyvalue ;
207 0         0 $memdlocal->delete($identlocal);
208             }
209            
210 0         0 $self->{opened} = 0;
211            
212             }
213              
214             sub close {
215 2     2 0 4 my $self = shift;
216              
217 2 50       8 if ($self->{opened}) {
218 2         6 $self->{opened} = 0;
219             }
220             }
221              
222             sub DESTROY {
223 2     2   3 my $self = shift;
224              
225 2 50       120 if ($self->{opened}) {
226             }
227             }
228              
229             1;
230              
231             =pod
232              
233             =head1 NAME
234              
235             Apache::Session::Store::Memorycached - Store persistent data on the network with memcached
236              
237             =head1 SYNOPSIS
238              
239              
240             use Apache::Session::Store::Memorycached;
241            
242             my $store = new Apache::Session::Store::Memorycached;
243            
244             $store->insert($ref);
245             $store->update($ref);
246             $store->materialize($ref);
247             $store->remove($ref);
248              
249             =head1 DESCRIPTION
250              
251             This module fulfills the storage interface of Apache::Session. The serialized
252             objects are stored in files on your network in unused memory
253              
254             =head1 OPTIONS
255              
256             This module requires one argument in the usual Apache::Session style. The
257             name of the option is servers, and the value is the same of memcached .
258             Example
259              
260             tie %s, 'Apache::Session::Memorycached', undef,
261             {servers => ['mymemcachedserver:port'],
262             'timeout' => '300',
263             'updateOnly' => 1 ,
264             'principal' => uid,
265             };
266              
267             In order to optimize the network ,you can use a local memcached server.
268             All read-only opération are sending fisrt at local server .If you need write ou rewrite data , the data is sending at the principal memcached sever and local cache too for synchronisation.
269              
270             note : 'updateOnly' => 1 just realize up-date operation not init operation.
271             Init operation is use in order to book and lock the number session but it's not available in this module
272            
273             'principal' => uid : this parameter is use to create reverse reference
274             like this : MD5_hex(uid) => id_session in memcached server . By this it usefull to retrieve id_session from principal name . And add uid_MD5 => MD5_hex(uid) in main session .
275            
276              
277             =head1 NOTES
278              
279              
280             =head1 AUTHOR
281              
282             This module was written by eric german
283              
284             =head1 SEE ALSO
285              
286             L