1 package OpenSRF::Utils::Cache;
2 use strict; use warnings;
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;
11 my $log = 'OpenSRF::Utils::Logger';
19 This class just subclasses Cache::Memcached.
20 see Cache::Memcached for more options.
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.
26 my $cache = OpenSRF::Utils::Cache->current("user");
27 $cache->set( "key1", "value1" [, $expire_secs ] );
28 my $val = $cache->get( "key1" );
37 # ------------------------------------------------------
38 # Persist methods and method names
39 # ------------------------------------------------------
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;
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";;
55 # ------------------------------------------------------
61 Return a named cache if it exists
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 );
75 Create a new named memcache object.
81 my( $class, $cache_type, $persist ) = @_;
82 $cache_type ||= 'global';
83 $class = ref( $class ) || $class;
85 return $caches{$cache_type} if (defined $caches{$cache_type});
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' );
91 $servers = [ $servers ] if(!ref($servers));
94 $self->{persist} = $persist || 0;
95 $self->{memcache} = Cache::Memcached->new( { servers => $servers } );
96 if(!$self->{memcache}) {
97 throw OpenSRF::EX::PANIC ("Unable to create a new memcache object for $cache_type");
100 bless($self, $class);
101 $caches{$cache_type} = $self;
111 my($self, $key, $value, $expiretime ) = @_;
113 $key = _clean_cache_key($key);
115 return undef unless( defined $key and defined $value );
117 $value = OpenSRF::Utils::JSON->perl2JSON($value);
119 if($self->{persist}){ _load_methods(); }
121 $expiretime ||= $max_persist_time;
123 unless( $self->{memcache}->set( $key, $value, $expiretime ) ) {
124 $log->error("Unable to store $key => [".length($value)." bytes] in memcached server" );
128 $log->debug("Stored $key => $value in memcached server", INTERNAL);
130 if($self->{"persist"}) {
132 my ($slot) = $persist_add_slot->run("_CACHEVAL_$key", $expiretime . "s");
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" );
140 #XXX destroy the slot and rebuild it to prevent DOS
144 ($slot) = $persist_push_stack->run("_CACHEVAL_$key", $value);
147 throw OpenSRF::EX::ERROR ("Unable to push data onto stack in persist slot _CACHEVAL_$key" );
160 my( $self, $key ) = @_;
161 $key = _clean_cache_key($key);
162 if(!$key) { return undef; }
163 if($self->{persist}){ _load_methods(); }
164 $self->{memcache}->delete($key);
165 if( $self->{persist} ) {
166 $persist_destroy_slot->run("_CACHEVAL_$key");
177 my($self, $key ) = @_;
179 $key = _clean_cache_key($key);
181 my $val = $self->{memcache}->get( $key );
182 return OpenSRF::Utils::JSON->JSON2perl($val) if defined($val);
184 if($self->{persist}){ _load_methods(); }
186 # if not in memcache but we are persisting, the put it into memcache
187 if( $self->{"persist"} ) {
188 $val = $persist_peek_stack->( "_CACHEVAL_$key" );
190 my ($expire) = $persist_slot_get_expire->run("_CACHEVAL_$key");
192 $self->{memcache}->set( $key, $val, $expire);
194 $self->{memcache}->set( $key, $val, $max_persist_time);
196 return OpenSRF::Utils::JSON->JSON2perl($val);
209 if(!$persist_add_slot) {
211 OpenSRF::Application->method_lookup($persist_add_slot_name);
212 if(!ref($persist_add_slot)) {
213 throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_add_slot_name");
217 if(!$persist_push_stack) {
218 $persist_push_stack =
219 OpenSRF::Application->method_lookup($persist_push_stack_name);
220 if(!ref($persist_push_stack)) {
221 throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_push_stack_name");
225 if(!$persist_peek_stack) {
226 $persist_peek_stack =
227 OpenSRF::Application->method_lookup($persist_peek_stack_name);
228 if(!ref($persist_peek_stack)) {
229 throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_peek_stack_name");
233 if(!$persist_destroy_slot) {
234 $persist_destroy_slot =
235 OpenSRF::Application->method_lookup($persist_destroy_slot_name);
236 if(!ref($persist_destroy_slot)) {
237 throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_destroy_slot_name");
240 if(!$persist_slot_get_expire) {
241 $persist_slot_get_expire =
242 OpenSRF::Application->method_lookup($persist_slot_get_expire_name);
243 if(!ref($persist_slot_get_expire)) {
244 throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_slot_get_expire_name");
247 if(!$persist_slot_find) {
249 OpenSRF::Application->method_lookup($persist_slot_find_name);
250 if(!ref($persist_slot_find)) {
251 throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_slot_find_name");
257 =head2 _clean_cache_key
259 Try to make the requested cache key conform to memcached's requirements. Per
260 https://github.com/memcached/memcached/blob/master/doc/protocol.txt:
263 Data stored by memcached is identified with the help of a key. A key
264 is a text string which should uniquely identify the data for clients
265 that are interested in storing and retrieving it. Currently the
266 length limit of a key is set at 250 characters (of course, normally
267 clients wouldn't need to use such long keys); the key must not include
268 control characters or whitespace.
273 sub _clean_cache_key {
276 $key =~ s{(\p{Cntrl}|\s)}{}g;