From 23870e526ebe98056329423af4e824f7f0afd70d Mon Sep 17 00:00:00 2001 From: David Cook Date: Fri, 15 Dec 2023 06:02:17 +0000 Subject: [PATCH] Bug 35579: Cache authorised value lookup by MARC field This patch adds a "get_descriptions_by_marc_field" method which caches AuthorisedValue descriptions when searched by MARC field, which is used when exporting MARC to CSV. Signed-off-by: David Nind --- C4/Record.pm | 14 ++++++++------ Koha/AuthorisedValues.pm | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/C4/Record.pm b/C4/Record.pm index 9f0d21c491..33d18b86bd 100644 --- a/C4/Record.pm +++ b/C4/Record.pm @@ -576,9 +576,12 @@ sub marcrecord2csv { # If it is a subfield my @loop_values; if (defined $tag->{subfieldtag} ) { - my $av = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, tagsubfield => $tag->{subfieldtag}, }); - $av = $av->count ? $av->unblessed : []; - my $av_description_mapping = { map { ( $_->{authorised_value} => $_->{lib} ) } @$av }; + my $av_description_mapping = Koha::AuthorisedValues->get_descriptions_by_marc_field( + { + frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, + tagsubfield => $tag->{subfieldtag}, + } + ); # For each field foreach my $field (@fields) { my @subfields = $field->subfield( $tag->{subfieldtag} ); @@ -589,9 +592,8 @@ sub marcrecord2csv { # Or a field } else { - my $av = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, }); - $av = $av->count ? $av->unblessed : []; - my $authvalues = { map { ( $_->{authorised_value} => $_->{lib} ) } @$av }; + my $authvalues = Koha::AuthorisedValues->get_descriptions_by_marc_field( + { frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, } ); foreach my $field ( @fields ) { my $value; diff --git a/Koha/AuthorisedValues.pm b/Koha/AuthorisedValues.pm index 822be037ea..377d1de552 100644 --- a/Koha/AuthorisedValues.pm +++ b/Koha/AuthorisedValues.pm @@ -141,6 +141,38 @@ sub get_descriptions_by_koha_field { return @descriptions; } +=head3 get_descriptions_by_marc_field + + Return cached descriptions when looking up by MARC field/subfield + +=cut + +sub get_descriptions_by_marc_field { + my ( $self, $params ) = @_; + my $frameworkcode = $params->{frameworkcode} || ''; + my $tagfield = $params->{tagfield}; + my $tagsubfield = $params->{tagsubfield}; + + return {} unless defined $params->{tagfield}; + + my $memory_cache = Koha::Cache::Memory::Lite->get_instance; + my $cache_key = "AV_descriptions_by_MARC:$frameworkcode:$tagfield"; + if ($tagsubfield) { + $cache_key .= ":$tagsubfield"; + } + + my $cached = $memory_cache->get_from_cache($cache_key); + return $cached if $cached; + + my $descriptions = {}; + my @avs = $self->search_by_marc_field($params)->as_list; + foreach my $av (@avs) { + $descriptions->{ $av->authorised_value } = $av->lib; + } + $memory_cache->set_in_cache( $cache_key, $descriptions ); + return $descriptions; +} + sub categories { my ( $self ) = @_; my $rs = $self->_resultset->search( -- 2.30.2