Debian Codegraph @

lemonldap-ng-common/lib/Lemonldap/NG/Common/Apache/Session/Store.pm

raw
1
2 package Lemonldap::NG::Common::Apache::Session::Store;
3
4 use strict;
5
6 our $VERSION = '2.19.0';
7
8 sub new {
9 my $class = shift;
10 return bless {}, $class;
11 }
12
13 sub insert {
14 my $self = shift;
15 my $session = shift;
16 $self->{args} = $session->{args};
17
18 # Store session in cache
19 my $id = $session->{data}->{_session_id};
20 $self->storeInCache( $id, $session->{serialized} );
21
22 # Store in session backend
23 return $self->module->insert($session);
24 }
25
26 sub update {
27 my $self = shift;
28 my $session = shift;
29 $self->{args} = $session->{args};
30
31 #TODO: remove cache on all LL::NG instances if updateCache == 1
32
33 unless ( defined( $session->{args}->{updateCache} )
34 and $session->{args}->{updateCache} == -1 )
35 {
36
37 # Update session in cache
38 my $id = $session->{data}->{_session_id};
39 $self->removeFromCache($id) if ( $self->getFromCache($id) );
40 $self->storeInCache( $id, $session->{serialized} );
41 }
42
43 unless ( defined( $session->{args}->{updateCache} )
44 and $session->{args}->{updateCache} == 2 )
45 {
46
47 # Update session in backend
48 return $self->module->update($session);
49 }
50 }
51
52 sub materialize {
53 my $self = shift;
54 my $session = shift;
55 $self->{args} = $session->{args};
56
57 # Get session from cache
58 my $id = $session->{data}->{_session_id};
59 if ( !$self->{args}->{noCache} and $self->getFromCache($id) ) {
60 $session->{serialized} = $self->getFromCache($id);
61 eval { JSON::from_json( $session->{serialized} ); };
62 if ($@) {
63 print STDERR "Local data corrupted, ignore cached session\n";
64 $session->{serialized} = undef;
65 }
66 else {
67 return;
68 }
69 }
70
71 # Get session from backend
72 $self->module->materialize($session);
73
74 # Store session in cache
75 $self->storeInCache( $id, $session->{serialized} );
76
77 return;
78 }
79
80 sub remove {
81 my $self = shift;
82 my $session = shift;
83 $self->{args} = $session->{args};
84
85 #TODO: remove cache on all LL::NG instances if updateCache == 1
86
87 unless ($session->{args}->{updateCache}
88 and $session->{args}->{updateCache} == -1 )
89 {
90
91 # Remove session from cache
92 my $id = $session->{data}->{_session_id};
93 $self->removeFromCache($id) if ( $self->getFromCache($id) );
94 }
95
96 unless ($session->{args}->{updateCache}
97 and $session->{args}->{updateCache} == 2 )
98 {
99
100 # Remove session from backend
101 return $self->module->remove($session);
102 }
103 }
104
105 sub close {
106 my $self = shift;
107 my $session = shift;
108 $self->{args} = $session->{args};
109
110 return $self->module->close;
111 }
112
113 sub module {
114 my $self = shift;
115 return $self->{args}->{object_store};
116 }
117
118 sub cache {
119 my $self = shift;
120
121 return $self->{cache} if $self->{cache};
122
123 my $module = $self->{args}->{localStorage};
124 eval "use $module;";
125 $self->{cache} = $module->new( $self->{args}->{localStorageOptions} );
126
127 return $self->{cache};
128 }
129
130 sub storeInCache { _cache_call( 'set', @_ ); }
131
132 sub getFromCache { _cache_call( 'get', @_ ); }
133
134 sub removeFromCache { _cache_call( 'remove', @_ ); }
135
136 sub _cache_call {
137 my ( $sub, $self, @args ) = @_;
138 my $res = eval { $self->cache->$sub(@args); };
139 print STDERR "Unable to use cache: $@\n" if $@;
140 return $res;
141 }
142
143 1;