From 1d61bb81e8d7ed8bdf869a550ebbcb16bd554c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nyl=C3=A9n?= Date: Mon, 3 May 2021 13:11:21 +0200 Subject: [PATCH 1/2] Bug 26587: Cache authorised values in C4::Items::GetItemsInfo to improve performance This patch caches the authorised value descriptions in C4::Items::GetItemsInfo while looping through items. Improves performance for biblios with very large number of items. To test: 1. Have a biblio with many items (1000's). 2. View in staff (detail.pl) and opac (opac-detail.pl). Note how long it takes to load. 3. Apply patch. 4. Repeat 2. Note that pages load faster. Sponsored-by: Lund University Library --- C4/Items.pm | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/C4/Items.pm b/C4/Items.pm index 726c7e4cd8..b074ecf3b5 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -758,9 +758,13 @@ sub GetItemsInfo { my $i = 0; my @results; my $serial; - my $userenv = C4::Context->userenv; my $want_not_same_branch = C4::Context->preference("IndependentBranches") && !C4::Context->IsSuperLibrarian(); + + my $av_cache = {};# Initializing a chache for auth. values looked up in loops below + foreach my $itemcol (qw/notforloan restricted stack/){ + $av_cache->{$itemcol} = {}; + } while ( my $data = $sth->fetchrow_hashref ) { if ( $data->{borrowernumber} && $want_not_same_branch) { $data->{'NOTSAMEBRANCH'} = $data->{'bcode'} ne $userenv->{branch}; @@ -770,18 +774,35 @@ sub GetItemsInfo { my $descriptions; # get notforloan complete status if applicable - $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => $data->{frameworkcode}, kohafield => 'items.notforloan', authorised_value => $data->{itemnotforloan} }); - $data->{notforloanvalue} = $descriptions->{lib} // ''; - $data->{notforloanvalueopac} = $descriptions->{opac_description} // ''; - + if (exists $av_cache->{notforloan}->{$data->{itemnotforloan} // ''} ){ + $data->{notforloanvalue} = $av_cache->{notforloan}->{$data->{itemnotforloan} // ''}->{lib}; + $data->{notforloanvalueopac} = $av_cache->{notforloan}->{$data->{itemnotforloan} // ''}->{opac_description}; + }else{ + $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => $data->{frameworkcode}, kohafield => 'items.notforloan', authorised_value => $data->{itemnotforloan} }); + $data->{notforloanvalue} = $descriptions->{lib} // ''; + $data->{notforloanvalueopac} = $descriptions->{opac_description} // ''; + $av_cache->{notforloan}->{$data->{itemnotforloan} // ''}->{lib} = $data->{notforloanvalue}; + $av_cache->{notforloan}->{$data->{itemnotforloan} // ''}->{opac_description} = $data->{notforloanvalueopac}; + } # get restricted status and description if applicable - $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => $data->{frameworkcode}, kohafield => 'items.restricted', authorised_value => $data->{restricted} }); - $data->{restrictedvalue} = $descriptions->{lib} // ''; - $data->{restrictedvalueopac} = $descriptions->{opac_description} // ''; - + if (exists $av_cache->{restricted}->{$data->{restricted} // ''} ) { + $data->{restrictedvalue} = $av_cache->{restricted}->{$data->{restricted} // ''}->{lib}; + $data->{restrictedvalueopac} = $av_cache->{restricted}->{$data->{restricted} // ''}->{opac_description}; + }else{ + $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => $data->{frameworkcode}, kohafield => 'items.restricted', authorised_value => $data->{restricted} }); + $data->{restrictedvalue} = $descriptions->{lib} // ''; + $data->{restrictedvalueopac} = $descriptions->{opac_description} // ''; + $av_cache->{restricted}->{$data->{restricted} // ''}->{lib} = $data->{restrictedvalue} ; + $av_cache->{restricted}->{$data->{restricted} // ''}->{opac_description} = $data->{restrictedvalueopac}; + } # my stack procedures - $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => $data->{frameworkcode}, kohafield => 'items.stack', authorised_value => $data->{stack} }); - $data->{stack} = $descriptions->{lib} // ''; + if (exists $av_cache->{stack}->{$data->{stack} // ''} ) { + $data->{stack} = $av_cache->{stack}->{$data->{stack} // ''}->{lib}; + }else{ + $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => $data->{frameworkcode}, kohafield => 'items.stack', authorised_value => $data->{stack} }); + $data->{stack} = $descriptions->{lib} // ''; + $av_cache->{stack}->{$data->{stack}}->{lib} = $data->{stack}; + } # Find the last 3 people who borrowed this item. my $sth2 = $dbh->prepare("SELECT * FROM old_issues,borrowers -- 2.25.1