From bd88453971d2f0b2c97b7ba1b728ab5a8922f1d2 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 8 Sep 2021 12:37:38 +0000 Subject: [PATCH] Bug 28436: Add local cache to Template Plugins for branches and itemtypes This patch simply stores the branches an itemtypes in a hash for lookup. This improves efficiency on pages where we are loading many items To test: 1 - Add ~1000 items to a record 2 - Note load time 3 - Apply patch 4 - Reload and note faster load 5 - Edit a branchname/itemtype description 6 - Reload page 7 - Confirm the updated name is reflected in display --- Koha/Template/Plugin/Branches.pm | 7 +++++-- Koha/Template/Plugin/ItemTypes.pm | 10 +++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Koha/Template/Plugin/Branches.pm b/Koha/Template/Plugin/Branches.pm index 92c83be68c..a19efeaca5 100644 --- a/Koha/Template/Plugin/Branches.pm +++ b/Koha/Template/Plugin/Branches.pm @@ -27,11 +27,14 @@ use C4::Koha; use C4::Context; use Koha::Libraries; +our %branches; + sub GetName { my ( $self, $branchcode ) = @_; - my $l = Koha::Libraries->find($branchcode); - return $l ? $l->branchname : q{}; + %branches = map { $_->{branchcode} => $_ } @{Koha::Libraries->search()->unblessed} unless %branches; + my $l = $branches{$branchcode}; + return $l ? $l->{branchname} : q{}; } sub GetLoggedInBranchcode { diff --git a/Koha/Template/Plugin/ItemTypes.pm b/Koha/Template/Plugin/ItemTypes.pm index 5b3cf1edeb..f0e6ed24c2 100644 --- a/Koha/Template/Plugin/ItemTypes.pm +++ b/Koha/Template/Plugin/ItemTypes.pm @@ -24,13 +24,17 @@ use base qw( Template::Plugin ); use Koha::ItemTypes; +our %itemtypes; + + sub GetDescription { my ( $self, $itemtypecode, $want_parent ) = @_; - my $itemtype = Koha::ItemTypes->find( $itemtypecode ); + %itemtypes = map { $_->{itemtype} => $_ } @{Koha::ItemTypes->search_with_localization()->unblessed} unless %itemtypes; + my $itemtype = $itemtypes{$itemtypecode}; return q{} unless $itemtype; my $parent; - $parent = $itemtype->parent if $want_parent; - return $parent ? $parent->translated_description . "->" . $itemtype->translated_description : $itemtype->translated_description; + $parent = $itemtypes{$itemtype->{parent_type}} if $want_parent; + return $parent ? $parent->{translated_description} . "->" . $itemtype->{translated_description} : $itemtype->{translated_description}; } sub Get { -- 2.20.1