From 6eabe663157b34df4953bf7cca738d5c2e5b9632 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 8 Sep 2021 12:37:38 +0000 Subject: [PATCH] Bug 29454: 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 Signed-off-by: David Nind --- Koha/Template/Plugin/Branches.pm | 13 ++++++++++--- Koha/Template/Plugin/ItemTypes.pm | 15 ++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Koha/Template/Plugin/Branches.pm b/Koha/Template/Plugin/Branches.pm index 92c83be68c..78a95e235a 100644 --- a/Koha/Template/Plugin/Branches.pm +++ b/Koha/Template/Plugin/Branches.pm @@ -27,11 +27,18 @@ use C4::Koha; use C4::Context; use Koha::Libraries; +our %branches; + +sub new { + my ($class, $context, @params) = @_; + bless { + map { $_->{branchcode} => $_ } @{Koha::Libraries->search()->unblessed} + }, $class; +} + sub GetName { my ( $self, $branchcode ) = @_; - - my $l = Koha::Libraries->find($branchcode); - return $l ? $l->branchname : q{}; + return $self->{$branchcode}->{branchname} // $branchcode; } sub GetLoggedInBranchcode { diff --git a/Koha/Template/Plugin/ItemTypes.pm b/Koha/Template/Plugin/ItemTypes.pm index 5b3cf1edeb..6f620ba09e 100644 --- a/Koha/Template/Plugin/ItemTypes.pm +++ b/Koha/Template/Plugin/ItemTypes.pm @@ -24,13 +24,22 @@ use base qw( Template::Plugin ); use Koha::ItemTypes; +our %itemtypes; + +sub new { + my ($class, $context, @params) = @_; + bless { + map { $_->{itemtype} => $_ } @{Koha::ItemTypes->search_with_localization()->unblessed} + }, $class; +} + sub GetDescription { my ( $self, $itemtypecode, $want_parent ) = @_; - my $itemtype = Koha::ItemTypes->find( $itemtypecode ); + my $itemtype = $self->{$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 = $self->{$itemtype->{parent_type}} if $want_parent; + return $parent ? $parent->{translated_description} . "->" . $itemtype->{translated_description} : $itemtype->{translated_description}; } sub Get { -- 2.20.1