]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perl/lib/OpenSRF/Utils/Cache.pm
Move the buildbot workdirs to a non-volatile directory
[OpenSRF.git] / src / perl / lib / OpenSRF / Utils / Cache.pm
1 package OpenSRF::Utils::Cache;
2 use strict; use warnings;
3 use base qw/OpenSRF/;
4 use Cache::Memcached;
5 use OpenSRF::Utils::Logger qw/:level/;
6 use OpenSRF::Utils::Config;
7 use OpenSRF::Utils::SettingsClient;
8 use OpenSRF::EX qw(:try);
9 use OpenSRF::Utils::JSON;
10
11 my $log = 'OpenSRF::Utils::Logger';
12
13 =head1 NAME
14
15 OpenSRF::Utils::Cache
16
17 =head1 SYNOPSIS
18
19 This class just subclasses Cache::Memcached.
20 see Cache::Memcached for more options.
21
22 The value passed to the call to current is the cache type
23 you wish to access.  The below example sets/gets data
24 from the 'user' cache.
25
26 my $cache = OpenSRF::Utils::Cache->current("user");
27 $cache->set( "key1", "value1" [, $expire_secs ] );
28 my $val = $cache->get( "key1" );
29
30
31 =cut
32
33 sub DESTROY {}
34
35 my %caches;
36
37 # ------------------------------------------------------
38 # Persist methods and method names
39 # ------------------------------------------------------
40 my $persist_add_slot; 
41 my $persist_push_stack;
42 my $persist_peek_stack;
43 my $persist_destroy_slot;
44 my $persist_slot_get_expire;
45 my $persist_slot_find;
46
47 my $max_persist_time;
48 my $persist_add_slot_name        = "opensrf.persist.slot.create_expirable";
49 my $persist_push_stack_name      = "opensrf.persist.stack.push";
50 my $persist_peek_stack_name      = "opensrf.persist.stack.peek";
51 my $persist_destroy_slot_name    = "opensrf.persist.slot.destroy";
52 my $persist_slot_get_expire_name = "opensrf.persist.slot.get_expire";
53 my $persist_slot_find_name       = "opensrf.persist.slot.find";;
54
55 # ------------------------------------------------------
56
57 =head1 METHODS
58
59 =head2 current
60
61 Return a named cache if it exists
62
63 =cut
64
65 sub current {
66         my ( $class, $c_type )  = @_;
67         return undef unless $c_type;
68         return $caches{$c_type} if exists $caches{$c_type};
69         return $caches{$c_type} = $class->new( $c_type );
70 }
71
72
73 =head2 new
74
75 Create a new named memcache object.
76
77 =cut
78
79 sub new {
80
81         my( $class, $cache_type, $persist ) = @_;
82         $cache_type ||= 'global';
83         $class = ref( $class ) || $class;
84
85         return $caches{$cache_type} if (defined $caches{$cache_type});
86
87         my $conf = OpenSRF::Utils::SettingsClient->new;
88         my $servers = $conf->config_value( cache => $cache_type => servers => 'server' );
89         $max_persist_time = $conf->config_value( cache => $cache_type => 'max_cache_time' );
90
91         $servers = [ $servers ] if(!ref($servers));
92
93         my $self = {};
94         $self->{persist} = $persist || 0;
95         $self->{memcache} = Cache::Memcached->new( { servers => $servers } ); 
96         if(!$self->{memcache}) {
97         open(CACHEFOO, '>', '/tmp/cached');
98         print CACHEFOO "De nada\n";
99         close(CACHEFOO);
100                 throw OpenSRF::EX::PANIC ("Unable to create a new memcache object for $cache_type");
101         }
102
103         bless($self, $class);
104         $caches{$cache_type} = $self;
105         return $self;
106 }
107
108
109 =head2 put_cache
110
111 =cut
112
113 sub put_cache {
114         my($self, $key, $value, $expiretime ) = @_;
115         return undef unless( defined $key and defined $value );
116
117         $value = OpenSRF::Utils::JSON->perl2JSON($value);
118
119         if($self->{persist}){ _load_methods(); }
120
121         $expiretime ||= $max_persist_time;
122
123         unless( $self->{memcache}->set( $key, $value, $expiretime ) ) {
124                 $log->error("Unable to store $key => [".length($value)." bytes]  in memcached server" );
125                 return undef;
126         }
127
128         $log->debug("Stored $key => $value in memcached server", INTERNAL);
129
130         if($self->{"persist"}) {
131
132                 my ($slot) = $persist_add_slot->run("_CACHEVAL_$key", $expiretime . "s");
133
134                 if(!$slot) {
135                         # slot may already exist
136                         ($slot) = $persist_slot_find->run("_CACHEVAL_$key");
137                         if(!defined($slot)) {
138                                 throw OpenSRF::EX::ERROR ("Unable to create cache slot $key in persist server" );
139                         } else {
140                                 #XXX destroy the slot and rebuild it to prevent DOS
141                         }
142                 }
143
144                 ($slot) = $persist_push_stack->run("_CACHEVAL_$key", $value);
145
146                 if(!$slot) {
147                         throw OpenSRF::EX::ERROR ("Unable to push data onto stack in persist slot _CACHEVAL_$key" );
148                 }
149         }
150
151         return $key;
152 }
153
154
155 =head2 delete_cache
156
157 =cut
158
159 sub delete_cache {
160         my( $self, $key ) = @_;
161         if(!$key) { return undef; }
162         if($self->{persist}){ _load_methods(); }
163         $self->{memcache}->delete($key);
164         if( $self->{persist} ) {
165                 $persist_destroy_slot->run("_CACHEVAL_$key");
166         }
167         return $key; 
168 }
169
170
171 =head2 get_cache
172
173 =cut
174
175 sub get_cache {
176         my($self, $key ) = @_;
177
178         my $val = $self->{memcache}->get( $key );
179         return OpenSRF::Utils::JSON->JSON2perl($val) if defined($val);
180
181         if($self->{persist}){ _load_methods(); }
182
183         # if not in memcache but we are persisting, the put it into memcache
184         if( $self->{"persist"} ) {
185                 $val = $persist_peek_stack->( "_CACHEVAL_$key" );
186                 if(defined($val)) {
187                         my ($expire) = $persist_slot_get_expire->run("_CACHEVAL_$key");
188                         if($expire)     {
189                                 $self->{memcache}->set( $key, $val, $expire);
190                         } else {
191                                 $self->{memcache}->set( $key, $val, $max_persist_time);
192                         }
193                         return OpenSRF::Utils::JSON->JSON2perl($val);
194                 }
195         }
196         return undef;
197 }
198
199
200 =head2 _load_methods
201
202 =cut
203
204 sub _load_methods {
205
206         if(!$persist_add_slot) {
207                 $persist_add_slot = 
208                         OpenSRF::Application->method_lookup($persist_add_slot_name);
209                 if(!ref($persist_add_slot)) {
210                         throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_add_slot_name");
211                 }
212         }
213
214         if(!$persist_push_stack) {
215                 $persist_push_stack = 
216                         OpenSRF::Application->method_lookup($persist_push_stack_name);
217                 if(!ref($persist_push_stack)) {
218                         throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_push_stack_name");
219                 }
220         }
221
222         if(!$persist_peek_stack) {
223                 $persist_peek_stack = 
224                         OpenSRF::Application->method_lookup($persist_peek_stack_name);
225                 if(!ref($persist_peek_stack)) {
226                         throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_peek_stack_name");
227                 }
228         }
229
230         if(!$persist_destroy_slot) {
231                 $persist_destroy_slot = 
232                         OpenSRF::Application->method_lookup($persist_destroy_slot_name);
233                 if(!ref($persist_destroy_slot)) {
234                         throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_destroy_slot_name");
235                 }
236         }
237         if(!$persist_slot_get_expire) {
238                 $persist_slot_get_expire = 
239                         OpenSRF::Application->method_lookup($persist_slot_get_expire_name);
240                 if(!ref($persist_slot_get_expire)) {
241                         throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_slot_get_expire_name");
242                 }
243         }
244         if(!$persist_slot_find) {
245                 $persist_slot_find = 
246                         OpenSRF::Application->method_lookup($persist_slot_find_name);
247                 if(!ref($persist_slot_find)) {
248                         throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_slot_find_name");
249                 }
250         }
251 }
252
253
254
255
256
257
258
259 1;
260