Bug 17301: Follow-up - Standardize headings
[koha.git] / Koha / Cache.pm
blob08577bdbb161b081244f5dc59729e21d0d8f71a1
1 package Koha::Cache;
3 # Copyright 2009 Chris Cormack and The Koha Dev Team
4 # Parts copyright 2012-2013 C & P Bibliography Services
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 =head1 NAME
23 Koha::Cache - Handling caching of html and Objects for Koha
25 =head1 SYNOPSIS
27 use Koha::Cache;
28 my $cache = Koha::Cache->new({cache_type => $cache_type, %params});
30 =head1 DESCRIPTION
32 Koha caching routines. This class provides two interfaces for cache access.
33 The first, traditional OO interface provides the following functions:
35 =head1 FUNCTIONS
37 =cut
38 use strict;
39 use warnings;
40 use Carp;
41 use Module::Load::Conditional qw(can_load);
42 use Sereal::Encoder;
43 use Sereal::Decoder;
45 use Koha::Cache::Object;
46 use Koha::Config;
48 use base qw(Class::Accessor);
50 __PACKAGE__->mk_ro_accessors(
51 qw( cache memcached_cache fastmmap_cache memory_cache ));
53 our %L1_cache;
54 our $L1_encoder = Sereal::Encoder->new;
55 our $L1_decoder = Sereal::Decoder->new;
57 =head2 get_instance
59 my $cache = Koha::Caches->get_instance();
61 This gets a shared instance of the cache, set up in a very default way. This is
62 the recommended way to fetch a cache object. If possible, it'll be
63 persistent across multiple instances.
65 =cut
67 =head2 new
69 Create a new Koha::Cache object. This is required for all cache-related functionality.
71 =cut
73 sub new {
74 my ( $class, $self, $params ) = @_;
75 $self->{'default_type'} =
76 $self->{cache_type}
77 || $ENV{CACHING_SYSTEM} # DELME What about this?
78 || 'memcached';
80 my $subnamespace = $params->{subnamespace} // '';
82 $ENV{DEBUG} && carp "Default caching system: $self->{'default_type'}";
84 $self->{'timeout'} ||= 0;
85 # Should we continue to support MEMCACHED ENV vars?
86 $self->{'namespace'} ||= $ENV{MEMCACHED_NAMESPACE};
87 my @servers = split /,/, $ENV{MEMCACHED_SERVERS} || '';
88 unless ( $self->{namespace} and @servers ) {
89 my $koha_config = Koha::Config->read_from_file( Koha::Config->guess_koha_conf() );
90 $self->{namespace} ||= $koha_config->{config}{memcached_namespace} || 'koha';
91 @servers = split /,/, $koha_config->{config}{memcached_servers} // ''
92 unless @servers;
94 $self->{namespace} .= ":$subnamespace:";
96 if ( $self->{'default_type'} eq 'memcached'
97 && can_load( modules => { 'Cache::Memcached::Fast' => undef } )
98 && _initialize_memcached($self, @servers)
99 && defined( $self->{'memcached_cache'} ) )
101 $self->{'cache'} = $self->{'memcached_cache'};
104 if ( $self->{'default_type'} eq 'fastmmap'
105 && defined( $ENV{GATEWAY_INTERFACE} )
106 && can_load( modules => { 'Cache::FastMmap' => undef } )
107 && _initialize_fastmmap($self)
108 && defined( $self->{'fastmmap_cache'} ) )
110 $self->{'cache'} = $self->{'fastmmap_cache'};
113 # Unless memcache or fastmmap has already been picked, use memory_cache
114 unless ( defined( $self->{'cache'} ) ) {
115 if ( can_load( modules => { 'Cache::Memory' => undef } )
116 && _initialize_memory($self) )
118 $self->{'cache'} = $self->{'memory_cache'};
122 $ENV{DEBUG} && carp "Selected caching system: " . ($self->{'cache'} // 'none');
124 return
125 bless $self,
126 $class;
129 sub _initialize_memcached {
130 my ($self, @servers) = @_;
132 return unless @servers;
134 $ENV{DEBUG}
135 && carp "Memcached server settings: "
136 . join( ', ', @servers )
137 . " with "
138 . $self->{'namespace'};
139 # Cache::Memcached::Fast doesn't allow a default expire time to be set
140 # so we force it on setting.
141 my $memcached = Cache::Memcached::Fast->new(
143 servers => \@servers,
144 compress_threshold => 10_000,
145 namespace => $self->{'namespace'},
146 utf8 => 1,
149 # Ensure we can actually talk to the memcached server
150 my $ismemcached = $memcached->set('ismemcached','1');
151 return $self unless $ismemcached;
152 $self->{'memcached_cache'} = $memcached;
153 return $self;
156 sub _initialize_fastmmap {
157 my ($self) = @_;
158 my ($cache, $share_file);
160 # Temporary workaround to catch fatal errors when: C4::Context module
161 # is not loaded beforehand, or Cache::FastMmap init fails for whatever
162 # other reason (e.g. due to permission issues - see Bug 13431)
163 eval {
164 $share_file = join( '-',
165 "/tmp/sharefile-koha", $self->{'namespace'},
166 C4::Context->config('hostname'), C4::Context->config('database') );
168 $cache = Cache::FastMmap->new(
169 'share_file' => $share_file,
170 'expire_time' => $self->{'timeout'},
171 'unlink_on_exit' => 0,
174 if ( $@ ) {
175 warn "FastMmap cache initialization failed: $@";
176 return;
178 return unless defined $cache;
179 $self->{'fastmmap_cache'} = $cache;
180 return $self;
183 sub _initialize_memory {
184 my ($self) = @_;
186 # Default cache time for memory is _always_ short unless it's specially
187 # defined, to allow it to work reliably in a persistent environment.
188 my $cache = Cache::Memory->new(
189 'namespace' => $self->{'namespace'},
190 'default_expires' => "$self->{'timeout'} sec" || "10 sec",
192 $self->{'memory_cache'} = $cache;
193 # Memory cache can't handle complex types for some reason, so we use its
194 # freeze and thaw functions.
195 $self->{ref($cache) . '_set'} = sub {
196 my ($key, $val, $exp) = @_;
197 # Refer to set_expiry in Cache::Entry for why we do this 'sec' thing.
198 $exp = "$exp sec" if defined $exp;
199 # Because we need to use freeze, it must be a reference type.
200 $cache->freeze($key, [$val], $exp);
202 $self->{ref($cache) . '_get'} = sub {
203 my $res = $cache->thaw(shift);
204 return unless defined $res;
205 return $res->[0];
207 return $self;
210 =head2 is_cache_active
212 Routine that checks whether or not a default caching method is active on this
213 object.
215 =cut
217 sub is_cache_active {
218 my $self = shift;
219 return $self->{'cache'} ? 1 : 0;
222 =head2 set_in_cache
224 $cache->set_in_cache($key, $value, [$options]);
226 Save a value to the specified key in the cache. A hashref of options may be
227 specified.
229 The possible options are:
231 =over
233 =item expiry
235 Expiry time of this cached entry in seconds.
237 =item cache
239 The cache object to use if you want to provide your own. It should be an
240 instance of C<Cache::*> and follow the same interface as L<Cache::Memcache>.
242 =back
244 =cut
246 sub set_in_cache {
247 my ( $self, $key, $value, $options, $_cache) = @_;
248 # This is a bit of a hack to support the old API in case things still use it
249 if (defined $options && (ref($options) ne 'HASH')) {
250 my $new_options;
251 $new_options->{expiry} = $options;
252 $new_options->{cache} = $_cache if defined $_cache;
253 $options = $new_options;
256 # the key mustn't contain whitespace (or control characters) for memcache
257 # but shouldn't be any harm in applying it globally.
258 $key =~ s/[\x00-\x20]/_/g;
260 my $cache = $options->{cache} || 'cache';
261 croak "No key" unless $key;
262 $ENV{DEBUG} && carp "set_in_cache for $key";
264 return unless ( $self->{$cache} && ref( $self->{$cache} ) =~ m/^Cache::/ );
265 my $expiry = $options->{expiry};
266 $expiry //= $self->{timeout};
267 my $set_sub = $self->{ref($self->{$cache}) . "_set"};
269 my $flag = '-CF0'; # 0: scalar, 1: frozen data structure
270 if (ref($value)) {
271 # Set in L1 cache as a data structure
272 # We only save the frozen form: we do want to save $value in L1
273 # directly in order to protect it. And thawing now may not be
274 # needed, so improves performance.
275 $value = $L1_encoder->encode($value);
276 $L1_cache{$self->{namespace}}{$key}->{frozen} = $value;
277 $flag = '-CF1';
278 } else {
279 # Set in L1 cache as a scalar; exit if we are caching an undef
280 $L1_cache{$self->{namespace}}{$key} = $value;
281 return if !defined $value;
284 $value .= $flag;
285 # We consider an expiry of 0 to be infinite
286 if ( $expiry ) {
287 return $set_sub
288 ? $set_sub->( $key, $value, $expiry )
289 : $self->{$cache}->set( $key, $value, $expiry );
291 else {
292 return $set_sub
293 ? $set_sub->( $key, $value )
294 : $self->{$cache}->set( $key, $value );
298 =head2 get_from_cache
300 my $value = $cache->get_from_cache($key, [ $options ]);
302 Retrieve the value stored under the specified key in the cache.
304 The possible options are:
306 =over
308 =item unsafe
310 If set, this will avoid performing a deep copy of the item. This
311 means that it won't be safe if something later modifies the result of the
312 function. It should be used with caution, and could save processing time
313 in some situations where is safe to use it. Make sure you know what you are doing!
315 =item cache
317 The cache object to use if you want to provide your own. It should be an
318 instance of C<Cache::*> and follow the same interface as L<Cache::Memcache>.
320 =back
322 =cut
324 sub get_from_cache {
325 my ( $self, $key, $options ) = @_;
326 my $cache = $options->{cache} || 'cache';
327 my $unsafe = $options->{unsafe} || 0;
328 $key =~ s/[\x00-\x20]/_/g;
329 croak "No key" unless $key;
330 $ENV{DEBUG} && carp "get_from_cache for $key";
331 return unless ( $self->{$cache} && ref( $self->{$cache} ) =~ m/^Cache::/ );
333 # Return L1 cache value if exists
334 if ( exists $L1_cache{$self->{namespace}}{$key} ) {
335 if (ref($L1_cache{$self->{namespace}}{$key})) {
336 if ($unsafe) {
337 # ONLY use thawed for unsafe calls !!!
338 $L1_cache{$self->{namespace}}{$key}->{thawed} ||= $L1_decoder->decode($L1_cache{$self->{namespace}}{$key}->{frozen});
339 return $L1_cache{$self->{namespace}}{$key}->{thawed};
340 } else {
341 return $L1_decoder->decode($L1_cache{$self->{namespace}}{$key}->{frozen});
343 } else {
344 # No need to thaw if it's a scalar
345 return $L1_cache{$self->{namespace}}{$key};
349 my $get_sub = $self->{ref($self->{$cache}) . "_get"};
350 my $L2_value = $get_sub ? $get_sub->($key) : $self->{$cache}->get($key);
352 return if ref($L2_value);
353 return unless (defined($L2_value) && length($L2_value) >= 4);
355 my $flag = substr($L2_value, -4, 4, '');
356 if ($flag eq '-CF0') {
357 # it's a scalar
358 $L1_cache{$self->{namespace}}{$key} = $L2_value;
359 return $L2_value;
360 } elsif ($flag eq '-CF1') {
361 # it's a frozen data structure
362 my $thawed;
363 eval { $thawed = $L1_decoder->decode($L2_value); };
364 return if $@;
365 $L1_cache{$self->{namespace}}{$key}->{frozen} = $L2_value;
366 # ONLY save thawed for unsafe calls !!!
367 $L1_cache{$self->{namespace}}{$key}->{thawed} = $thawed if $unsafe;
368 return $thawed;
371 # Unknown value / data type returned from L2 cache
372 return;
375 =head2 clear_from_cache
377 $cache->clear_from_cache($key);
379 Remove the value identified by the specified key from the default cache.
381 =cut
383 sub clear_from_cache {
384 my ( $self, $key, $cache ) = @_;
385 $key =~ s/[\x00-\x20]/_/g;
386 $cache ||= 'cache';
387 croak "No key" unless $key;
388 return unless ( $self->{$cache} && ref( $self->{$cache} ) =~ m/^Cache::/ );
390 # Clear from L1 cache
391 delete $L1_cache{$self->{namespace}}{$key};
393 return $self->{$cache}->delete($key)
394 if ( ref( $self->{$cache} ) =~ m'^Cache::Memcached' );
395 return $self->{$cache}->remove($key);
398 =head2 flush_all
400 $cache->flush_all();
402 Clear the entire default cache.
404 =cut
406 sub flush_all {
407 my ( $self, $cache ) = shift;
408 $cache ||= 'cache';
409 return unless ( $self->{$cache} && ref( $self->{$cache} ) =~ m/^Cache::/ );
411 $self->flush_L1_cache();
413 return $self->{$cache}->flush_all()
414 if ( ref( $self->{$cache} ) =~ m'^Cache::Memcached' );
415 return $self->{$cache}->clear();
418 sub flush_L1_cache {
419 my( $self ) = @_;
420 delete $L1_cache{$self->{namespace}};
423 =head1 TIED INTERFACE
425 Koha::Cache also provides a tied interface which enables users to provide a
426 constructor closure and (after creation) treat cached data like normal reference
427 variables and rely on the cache Just Working and getting updated when it
428 expires, etc.
430 my $cache = Koha::Cache->new();
431 my $data = 'whatever';
432 my $scalar = Koha::Cache->create_scalar(
434 'key' => 'whatever',
435 'timeout' => 2,
436 'constructor' => sub { return $data; },
439 print "$$scalar\n"; # Prints "whatever"
440 $data = 'somethingelse';
441 print "$$scalar\n"; # Prints "whatever" because it is cached
442 sleep 2; # Wait until the cache entry has expired
443 print "$$scalar\n"; # Prints "somethingelse"
445 my $hash = Koha::Cache->create_hash(
447 'key' => 'whatever',
448 'timeout' => 2,
449 'constructor' => sub { return $data; },
452 print "$$variable\n"; # Prints "whatever"
454 The gotcha with this interface, of course, is that the variable returned by
455 create_scalar and create_hash is a I<reference> to a tied variable and not a
456 tied variable itself.
458 The tied variable is configured by means of a hashref passed in to the
459 create_scalar and create_hash methods. The following parameters are supported:
461 =over
463 =item I<key>
465 Required. The key to use for identifying the variable in the cache.
467 =item I<constructor>
469 Required. A closure (or reference to a function) that will return the value that
470 needs to be stored in the cache.
472 =item I<preload>
474 Optional. A closure (or reference to a function) that gets run to initialize
475 the cache when creating the tied variable.
477 =item I<arguments>
479 Optional. Array reference with the arguments that should be passed to the
480 constructor function.
482 =item I<timeout>
484 Optional. The cache timeout in seconds for the variable. Defaults to 600
485 (ten minutes).
487 =item I<cache_type>
489 Optional. Which type of cache to use for the variable. Defaults to whatever is
490 set in the environment variable CACHING_SYSTEM. If set to 'null', disables
491 caching for the tied variable.
493 =item I<allowupdate>
495 Optional. Boolean flag to allow the variable to be updated directly. When this
496 is set and the variable is used as an l-value, the cache will be updated
497 immediately with the new value. Using this is probably a bad idea on a
498 multi-threaded system. When I<allowupdate> is not set to true, using the
499 tied variable as an l-value will have no effect.
501 =item I<destructor>
503 Optional. A closure (or reference to a function) that should be called when the
504 tied variable is destroyed.
506 =item I<unset>
508 Optional. Boolean flag to tell the object to remove the variable from the cache
509 when it is destroyed or goes out of scope.
511 =item I<inprocess>
513 Optional. Boolean flag to tell the object not to refresh the variable from the
514 cache every time the value is desired, but rather only when the I<local> copy
515 of the variable is older than the timeout.
517 =back
519 =head2 create_scalar
521 my $scalar = Koha::Cache->create_scalar(\%params);
523 Create scalar tied to the cache.
525 =cut
527 sub create_scalar {
528 my ( $self, $args ) = @_;
530 $self->_set_tied_defaults($args);
532 tie my $scalar, 'Koha::Cache::Object', $args;
533 return \$scalar;
536 sub create_hash {
537 my ( $self, $args ) = @_;
539 $self->_set_tied_defaults($args);
541 tie my %hash, 'Koha::Cache::Object', $args;
542 return \%hash;
545 sub _set_tied_defaults {
546 my ( $self, $args ) = @_;
548 $args->{'timeout'} = '600' unless defined( $args->{'timeout'} );
549 $args->{'inprocess'} = '0' unless defined( $args->{'inprocess'} );
550 unless ( $args->{cache_type} and lc( $args->{cache_type} ) eq 'null' ) {
551 $args->{'cache'} = $self;
552 $args->{'cache_type'} ||= $ENV{'CACHING_SYSTEM'};
555 return $args;
558 =head1 EXPORT
560 None by default.
562 =head1 SEE ALSO
564 Koha::Cache::Object
566 =head1 AUTHOR
568 Chris Cormack, E<lt>chris@bigballofwax.co.nzE<gt>
569 Paul Poulain, E<lt>paul.poulain@biblibre.comE<gt>
570 Jared Camins-Esakov, E<lt>jcamins@cpbibliography.comE<gt>
572 =cut
576 __END__