From 1878cb0bdf005a0363baa08a3ae19d5f5a48bb8e Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Wed, 9 Apr 2014 15:50:37 +1200 Subject: [PATCH] Bug 9967 - Cache authorised value requests properly Authorised values are now cached using the proper Koha::Cache mechanism, rather than a simple internal cache. Memoize has been removed because it didn't really work like we needed it to. Test plan: * running a persistent environment: * load the edit item screen * refresh several times to ensure any process-based caches are filled * add a new LOC authorised value as fast as possible (prepare it on another tab.) * refresh the edit item page * note that the new LOC value may or may not be showing in the "shelving location" * if more than 5 seconds have passed since saving it, it must now show up. * refresh a few times to ensure that it's showing on all processes. Note: * This patch depends on the caching changes of 9967. Signed-off-by: Brendan Gallagher Signed-off-by: Jonathan Druart Signed-off-by: Tomas Cohen Arazi --- C4/Koha.pm | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/C4/Koha.pm b/C4/Koha.pm index ee4bd67d4d..2e47cc94e5 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -25,8 +25,8 @@ use strict; use C4::Context; use C4::Branch qw(GetBranchesCount); +use Koha::Cache; use Koha::DateUtils qw(dt_from_string); -use Memoize; use DateTime::Format::MySQL; use Business::ISBN; use autouse 'Data::Dumper' => qw(Dumper); @@ -83,9 +83,6 @@ BEGIN { @EXPORT_OK = qw( GetDailyQuote ); } -# expensive functions -memoize('GetAuthorisedValues'); - =head1 NAME C4::Koha - Perl Module containing convenience functions for Koha scripts @@ -1126,13 +1123,33 @@ This function returns all authorised values from the'authorised_value' table in C<$category> returns authorised values for just one category (optional). +C<$selected> adds a "selected => 1" entry to the hash if the +authorised_value matches it. B this feature should be considered +deprecated as it may be removed in the future. + C<$opac> If set to a true value, displays OPAC descriptions rather than normal ones when they exist. =cut sub GetAuthorisedValues { my ( $category, $selected, $opac ) = @_; - my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : ""; + + # TODO: the "selected" feature should be replaced by a utility function + # somewhere else, it doesn't belong in here. For starters it makes + # caching much more complicated. Or just let the UI logic handle it, it's + # what it's for. + + # Is this cached already? + $opac = $opac ? 1 : 0; # normalise to be safe + my $selected_key = defined($selected) ? $selected : ''; + my $cache_key = "AuthorisedValues-$category-$selected_key-$opac"; + my $cache = Koha::Cache->get_instance(); + my $result = $cache->get_from_cache($cache_key); +warn "fetched $result from cache"; + return $result if $result; + + my $branch_limit = + C4::Context->userenv ? C4::Context->userenv->{"branch"} : ""; my @results; my $dbh = C4::Context->dbh; my $query = qq{ @@ -1178,6 +1195,11 @@ sub GetAuthorisedValues { push @results, $data; } $sth->finish; + + # We can't cache for long because of that "selected" thing which + # makes it impossible to clear the cache without iterating through every + # value, which sucks. This'll cover this request, and not a whole lot more. + $cache->set_in_cache( $cache_key, \@results, { deepcopy => 1, expiry => 5 } ); return \@results; } -- 2.39.5