From e578dccb0695acd5c7ba758e25576152f751844f 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. --- C4/Koha.pm | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/C4/Koha.pm b/C4/Koha.pm index 2db8c5b..dc9528b 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -25,9 +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 Memoize::Expire; use DateTime::Format::MySQL; use autouse 'Data::Dumper' => qw(Dumper); use DBI qw(:sql_types); @@ -78,11 +77,6 @@ BEGIN { @EXPORT_OK = qw( GetDailyQuote ); } -# expensive functions -tie my %memoize_cache => 'Memoize::Expire', - LIFETIME => 10; -memoize('GetAuthorisedValues', SCALAR_HASH => [HASH => \%memoize_cache]); - =head1 NAME C4::Koha - Perl Module containing convenience functions for Koha scripts @@ -1027,13 +1021,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{ @@ -1079,6 +1093,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; } @@ -1257,9 +1276,6 @@ Create a new authorised value. sub AddAuthorisedValue { my ($category, $authorised_value, $lib, $lib_opac, $imageurl) = @_; - # clear the memoize cache - flush_cache('AddAuthorisedValue'); - my $dbh = C4::Context->dbh; my $query = qq{ INSERT INTO authorised_values (category, authorised_value, lib, lib_opac, imageurl) -- 1.8.3.2