Bug 11998: Add a L1 cache for sysprefs

Accessing to the cache for each call to C4::Context->preference might
have an impact on performances.
To avoid that this patch introduces a L1 cache (simple hashref). It will
be populated by accessing the L2 cache (Koha::Cache).
If a pref is retrieved 10x, the first one will get the value from the L2
cache, then the L1 cache will be check.
To do so we will need to clear the L1 cache every time a page is loaded.

Signed-off-by: Tomas Cohen Arazi <tomascohen@unc.edu.ar>
Signed-off-by: Jacek Ablewicz <abl@biblos.pk.edu.pl>

Signed-off-by: Brendan Gallagher brendan@bywatersolutions.com
This commit is contained in:
Jonathan Druart 2016-03-10 08:55:27 +00:00 committed by Brendan Gallagher
parent 2eaf2cd71b
commit 47fbbb7cf5
1 changed files with 26 additions and 7 deletions

View File

@ -505,6 +505,7 @@ with this method.
=cut
my $syspref_cache = Koha::Cache->get_instance();
my %syspref_L1_cache;
my $use_syspref_cache = 1;
sub preference {
my $self = shift;
@ -512,13 +513,16 @@ sub preference {
$var = lc $var;
# Return the value if the var has already been accessed
if ($use_syspref_cache && exists $syspref_L1_cache{$var}) {
return $syspref_L1_cache{$var};
}
my $cached_var = $use_syspref_cache
? $syspref_cache->get_from_cache("syspref_$var")
: undef;
return $cached_var if defined $cached_var;
my $dbh = C4::Context->dbh or return 0;
my $value;
if ( defined $ENV{"OVERRIDE_SYSPREF_$var"} ) {
$value = $ENV{"OVERRIDE_SYSPREF_$var"};
@ -528,7 +532,10 @@ sub preference {
$value = $syspref ? $syspref->value() : undef;
}
$syspref_cache->set_in_cache("syspref_$var", $value) if $use_syspref_cache;
if ( $use_syspref_cache ) {
$syspref_cache->set_in_cache("syspref_$var", $value);
$syspref_L1_cache{$var} = $value;
}
return $value;
}
@ -581,7 +588,13 @@ will not be seen by this process.
=cut
sub clear_syspref_cache {
$syspref_cache->flush_all if $use_syspref_cache;
return unless $use_syspref_cache;
$syspref_cache->flush_all;
clear_syspref_L1_cache()
}
sub clear_syspref_L1_cache {
%syspref_L1_cache = ();
}
=head2 set_preference
@ -632,8 +645,10 @@ sub set_preference {
)->store();
}
$syspref_cache->set_in_cache( "syspref_$variable", $value )
if $use_syspref_cache;
if ( $use_syspref_cache ) {
$syspref_cache->set_in_cache( "syspref_$variable", $value );
$syspref_L1_cache{$variable} = $value;
}
return $syspref;
}
@ -652,7 +667,11 @@ sub delete_preference {
my ( $self, $var ) = @_;
if ( Koha::Config::SysPrefs->find( $var )->delete ) {
$syspref_cache->clear_from_cache("syspref_$var") if $use_syspref_cache;
if ( $use_syspref_cache ) {
$syspref_cache->clear_from_cache("syspref_$var");
delete $syspref_L1_cache{$var};
}
return 1;
}
return 0;