From f41d6974baf0e17af5f044f56c421d717aee037e Mon Sep 17 00:00:00 2001 From: Fridolin Somers Date: Fri, 4 Sep 2020 17:25:24 +0200 Subject: [PATCH] Bug 26393: Add cache on Koha::CirculationRules::get_effective_rule For performance of several circulation pages we should cache the result of method Koha::CirculationRules::get_effective_rule. Adds a constant CACHE_NO_CIRCULATION_RULE_VALUE to store a special value when method must return undef. --- Koha/CirculationRules.pm | 19 +++++++++++++++++++ admin/smart-rules.pl | 1 + 2 files changed, 20 insertions(+) diff --git a/Koha/CirculationRules.pm b/Koha/CirculationRules.pm index 87062f541a..9e7d79880f 100644 --- a/Koha/CirculationRules.pm +++ b/Koha/CirculationRules.pm @@ -26,6 +26,8 @@ use Koha::CirculationRule; use base qw(Koha::Objects); use constant GUESSED_ITEMTYPES_KEY => 'Koha_IssuingRules_last_guess'; +use constant EFFECTIVE_RULE_KEY => 'Koha_CirculationRule_get_effective_rule-'; +use constant CACHE_NO_CIRCULATION_RULE_VALUE => 'NONE'; =head1 NAME @@ -190,6 +192,21 @@ sub get_effective_rule { "Required parameter 'rule_name' missing") unless $rule_name; + my $cache = Koha::Caches->get_instance(); + my $cache_key = EFFECTIVE_RULE_KEY.$rule_name; + foreach ( $branchcode, $categorycode, $itemtype ) { + $cache_key .= '-'.($_ ? $_ : ''); + } + my $cached = $cache->get_from_cache($cache_key); + if ($cached) { + if ( ref $cached eq 'SCALAR' && $cached eq CACHE_NO_CIRCULATION_RULE_VALUE ) { + return undef; + } + my $cr = Koha::CirculationRule->new($cached); + $cr->{_result}->in_storage(1); # Tell it exists in DB + return $cr; + } + for my $v ( $branchcode, $categorycode, $itemtype ) { $v = undef if $v and $v eq '*'; } @@ -212,6 +229,8 @@ sub get_effective_rule { } )->single; + $cache->set_in_cache( $cache_key, $rule ? $rule->unblessed : CACHE_NO_CIRCULATION_RULE_VALUE ); + return $rule; } diff --git a/admin/smart-rules.pl b/admin/smart-rules.pl index 4a4727e5c2..f33eda763a 100755 --- a/admin/smart-rules.pl +++ b/admin/smart-rules.pl @@ -69,6 +69,7 @@ my $language = C4::Languages::getlanguage(); my $cache = Koha::Caches->get_instance; $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY ); +$cache->clear_from_cache( Koha::CirculationRules::EFFECTIVE_RULE_KEY ); if ($op eq 'delete') { my $itemtype = $input->param('itemtype'); -- 2.27.0