1 package OpenSRF::Utils::Cache;
2 use strict; use warnings;
3 use base qw/Cache::Memcached OpenSRF/;
5 use OpenSRF::Utils::Config;
6 use OpenSRF::Utils::SettingsClient;
7 use OpenSRF::EX qw(:try);
11 =head OpenSRF::Utils::Cache
13 This class just subclasses Cache::Memcached.
14 see Cache::Memcached for more options.
16 The value passed to the call to current is the cache type
17 you wish to access. The below example sets/gets data
18 from the 'user' cache.
20 my $cache = OpenSRF::Utils::Cache->current("user");
21 $cache->set( "key1", "value1" [, $expire_secs ] );
22 my $val = $cache->get( "key1" );
31 # ------------------------------------------------------
32 # Persist methods and method names
33 # ------------------------------------------------------
35 my $persist_push_stack;
36 my $persist_peek_stack;
37 my $persist_destroy_slot;
38 my $persist_slot_get_expire;
39 my $persist_slot_find;
41 my $max_persist_time = 86400;
42 my $persist_add_slot_name = "opensrf.persist.slot.create_expirable";
43 my $persist_push_stack_name = "opensrf.persist.stack.push";
44 my $persist_peek_stack_name = "opensrf.persist.stack.peek";
45 my $persist_destroy_slot_name = "opensrf.persist.slot.destroy";
46 my $persist_slot_get_expire_name = "opensrf.persist.slot.get_expire";
47 my $persist_slot_find_name = "opensrf.persist.slot.find";;
49 # ------------------------------------------------------
52 # return a named cache if it exists
54 my ( $class, $c_type ) = @_;
55 return undef unless $c_type;
56 return $caches{$c_type} if exists $caches{$c_type};
57 return $caches{$c_type} = $class->new( $c_type );
61 # create a new named memcache object.
64 my( $class, $cache_type, $persist ) = @_;
65 $cache_type ||= 'global';
66 $class = ref( $class ) || $class;
68 return $caches{$cache_type}
69 if (defined $caches{$cache_type});
71 my $conf = OpenSRF::Utils::SettingsClient->new;
72 my $servers = $conf->config_value( cache => $cache_type => servers => 'server' );
73 my $expire_time = $conf->config_value( cache => $cache_type => servers => 'max_cache_time' );
77 $self->{persist} = $persist || 0;
78 $self->{memcache} = Cache::Memcached->new( { servers => $servers } );
79 if(!$self->{memcache}) {
80 throw OpenSRF::EX::PANIC ("Unable to create a new memcache object for $cache_type");
84 $caches{$cache_type} = $self;
91 my($self, $key, $value, $expiretime ) = @_;
92 return undef unless( defined $key and defined $value );
94 $value = JSON->perl2JSON($value);
96 if($self->{persist}){ _load_methods(); }
98 $expiretime ||= $max_persist_time;
100 $self->{memcache}->set( $key, $value, $expiretime );
102 if($self->{"persist"}) {
104 my ($slot) = $persist_add_slot->run("_CACHEVAL_$key", $expiretime . "s");
107 # slot may already exist
108 ($slot) = $persist_slot_find->run("_CACHEVAL_$key");
109 if(!defined($slot)) {
110 throw OpenSRF::EX::ERROR ("Unable to create cache slot $key in persist server" );
112 #XXX destroy the slot and rebuild it to prevent DOS
116 ($slot) = $persist_push_stack->run("_CACHEVAL_$key", $value);
119 throw OpenSRF::EX::ERROR ("Unable to push data onto stack in persist slot _CACHEVAL_$key" );
127 my( $self, $key ) = @_;
128 if(!$key) { return undef; }
129 if($self->{persist}){ _load_methods(); }
130 $self->{memcache}->delete($key);
131 if( $self->{persist} ) {
132 $persist_destroy_slot->run("_CACHEVAL_$key");
138 my($self, $key ) = @_;
140 my $val = $self->{memcache}->get( $key );
141 return $val if defined($val);
143 if($self->{persist}){ _load_methods(); }
145 # if not in memcache but we are persisting, the put it into memcache
146 if( $self->{"persist"} ) {
147 $val = $persist_peek_stack->( "_CACHEVAL_$key" );
149 my ($expire) = $persist_slot_get_expire->run("_CACHEVAL_$key");
151 $self->{memcache}->set( $key, $val, $expire);
153 $self->{memcache}->set( $key, $val, $max_persist_time);
155 return JSON->JSON2perl($val);
166 if(!$persist_add_slot) {
168 OpenSRF::Application->method_lookup($persist_add_slot_name);
169 if(!ref($persist_add_slot)) {
170 throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_add_slot_name");
174 if(!$persist_push_stack) {
175 $persist_push_stack =
176 OpenSRF::Application->method_lookup($persist_push_stack_name);
177 if(!ref($persist_push_stack)) {
178 throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_push_stack_name");
182 if(!$persist_peek_stack) {
183 $persist_peek_stack =
184 OpenSRF::Application->method_lookup($persist_peek_stack_name);
185 if(!ref($persist_peek_stack)) {
186 throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_peek_stack_name");
190 if(!$persist_destroy_slot) {
191 $persist_destroy_slot =
192 OpenSRF::Application->method_lookup($persist_destroy_slot_name);
193 if(!ref($persist_destroy_slot)) {
194 throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_destroy_slot_name");
197 if(!$persist_slot_get_expire) {
198 $persist_slot_get_expire =
199 OpenSRF::Application->method_lookup($persist_slot_get_expire_name);
200 if(!ref($persist_slot_get_expire)) {
201 throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_slot_get_expire_name");
204 if(!$persist_slot_find) {
206 OpenSRF::Application->method_lookup($persist_slot_find_name);
207 if(!ref($persist_slot_find)) {
208 throw OpenSRF::EX::PANIC ("Unable to retrieve method $persist_slot_find_name");