From 312adcbc0557383dd4b6190dfa04999cd22ba914 Mon Sep 17 00:00:00 2001 From: David Gustafsson Date: Thu, 11 May 2023 15:12:20 +0200 Subject: [PATCH] Bug 31856: Refactor GetAuthorisedValueDesc --- C4/Biblio.pm | 32 +++++++++++--------------------- Koha/AuthorisedValue.pm | 14 ++++++-------- Koha/AuthorisedValues.pm | 37 +++++++++++++++++++------------------ 3 files changed, 36 insertions(+), 47 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index dbb4026816c..1102d039514 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -114,6 +114,7 @@ use Koha::SearchEngine; use Koha::SearchEngine::Indexer; use Koha::Libraries; use Koha::Util::MARC; +use Koha::AuthorisedValues; =head1 NAME @@ -1444,28 +1445,17 @@ sub GetAuthorisedValueDesc { } my $dbh = C4::Context->dbh; - if ( $category ne "" ) { - $cache_key = "AV_descriptions:" . $category; - my $av_descriptions = $cache->get_from_cache( $cache_key, { unsafe => 1 } ); - if ( !$av_descriptions ) { - $av_descriptions = { - map { - $_->authorised_value => - { lib => $_->lib, lib_opac => $_->lib_opac } - } Koha::AuthorisedValues->search( - { category => $category }, - { - columns => [ 'authorised_value', 'lib_opac', 'lib' ] - } - )->as_list - }; - $cache->set_in_cache($cache_key, $av_descriptions); - } - return ( $opac && $av_descriptions->{$value}->{'lib_opac'} ) - ? $av_descriptions->{$value}->{'lib_opac'} - : $av_descriptions->{$value}->{'lib'}; + if ( $category ) { + my $description = Koha::AuthorisedValues->get_description_by_category_and_authorised_value( + { + category => $category, + authorised_value => $value + } + ); + return $opac ? + $description->{'opac_description'} : $description->{'lib'}; } else { - return $value; # if nothing is found return the original value + return $value; # if nothing is found return the original value } } diff --git a/Koha/AuthorisedValue.pm b/Koha/AuthorisedValue.pm index da25d5e0584..eed399f944c 100644 --- a/Koha/AuthorisedValue.pm +++ b/Koha/AuthorisedValue.pm @@ -52,18 +52,15 @@ sub store { } else { my %updated_columns = $self->_result->get_dirty_columns; - - if ( exists $updated_columns{lib} - or exists $updated_columns{lib_opac} ) - { - $flush = 1; - } + $flush = exists $updated_columns{lib} + or exists $updated_columns{lib_opac}; } $self = $self->SUPER::store; if ($flush) { - my $key = "AV_descriptions:".$self->category; + my $category = $self->category; + my $key = "AV_descriptions:$category"; $cache->clear_from_cache($key); } @@ -78,7 +75,8 @@ AuthorisedValue specific C to clear relevant caches on delete. sub delete { my $self = shift @_; - my $key = "AV_descriptions:".$self->category; + my $category = $self->category; + my $key = "AV_descriptions:$category"; $cache->clear_from_cache($key); $self->SUPER::delete(@_); } diff --git a/Koha/AuthorisedValues.pm b/Koha/AuthorisedValues.pm index 69e8c5bd758..a41ee6549ef 100644 --- a/Koha/AuthorisedValues.pm +++ b/Koha/AuthorisedValues.pm @@ -143,30 +143,31 @@ sub get_descriptions_by_koha_field { sub get_description_by_category_and_authorised_value { my ( $self, $params ) = @_; - return unless defined $params->{category} && defined $params->{authorised_value}; + return unless defined $params->{category} and defined $params->{authorised_value}; my $category = $params->{category}; my $value = $params->{authorised_value}; - my $memory_cache = Koha::Cache::Memory::Lite->get_instance; - my $cache_key = "AV_description_by_category_and_authorised_value:$category:$value"; - my $description = $memory_cache->get_from_cache($cache_key); + my $cache = Koha::Caches->get_instance(); + my $cache_key = "AV_descriptions:$category"; + my $descriptions = $cache->get_from_cache($cache_key, { unsafe => 1 }); - unless (defined $description) { - my $av = $self->search( - { - category => $category, - authorised_value => $value - } - )->next; - $description = defined $av ? { - lib => $av->lib, - opac_description => $av->opac_description - } : {}; - $memory_cache->set_in_cache( $cache_key, $description ); + unless (defined $descriptions) { + $descriptions = { + map { + $_->authorised_value => { + lib => $_->lib, + opac_description => $_->opac_description + } + } $self->search( + { + category => $category + } + )->as_list + }; + $cache->set_in_cache($cache_key, $descriptions); } - - return $description; + return defined $descriptions->{$value} ? $descriptions->{$value} : {}; } sub categories { -- 2.40.0