From 1c8291f8aed2d4bfd94548dfc33e2b24d18cc1de 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. Test plan: 0. Do not apply the patch yet! 1. Go to http://localhost:8081/cgi-bin/koha/tools/csv-profiles.pl 2. Add a CSV profile with the default values and using the following for the "Profile MARC fields": 000|001|003|005|006|007|008|010|015|016|020|022|040|050|082|100|110|111|130|240|243|245|246|247|250|260|300|336|337|338|440|490|500|501|505|520|530|600|610|611|630|648|650|651|690|700|710|711|856|887|942|995|952|952$c 3. Create a new List 4. Add all the database's bibs to that list using SQL like the following (where the shelfnumber equals the number for your list): insert into virtualshelfcontents (shelfnumber,biblionumber,borrowernumber) select 1,biblionumber,51 from biblio; 5. Go to that list in the staff interface 6. Download that list using your CSV profile 7. Apply the patch 8. koha-plack --reload kohadev 9. Download that list using your CSV profile 10. Note that the output is the same, but that the output completes much more quickly after applying the patch Signed-off-by: David Nind Signed-off-by: Julian Maurice Signed-off-by: Katrin Fischer (cherry picked from commit 78c0e8df8da008c87f28d9432e16790ce07eabc1) Signed-off-by: Fridolin Somers (cherry picked from commit 423c24033999d2d098ae7c68956d3b583523500f) Signed-off-by: Lucas Gass --- 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 69a60f3520..8dfccb3447 100644 --- a/C4/Record.pm +++ b/C4/Record.pm @@ -578,9 +578,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} ); @@ -591,9 +594,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.39.5