diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 879044b0e4..b24a0a7c5f 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -1833,10 +1833,16 @@ sub UpsertMarcControlField { sub GetFrameworkCode { my ($biblionumber) = @_; - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?"); - $sth->execute($biblionumber); - my ($frameworkcode) = $sth->fetchrow; + my $cache = Koha::Cache::Memory::Lite->get_instance(); + my $cache_key = "FrameworkCode-$biblionumber"; + my $frameworkcode = $cache->get_from_cache($cache_key); + unless (defined $frameworkcode) { + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?"); + $sth->execute($biblionumber); + ($frameworkcode) = $sth->fetchrow; + $cache->set_in_cache($cache_key, $frameworkcode); + } return $frameworkcode; } @@ -2558,6 +2564,9 @@ sub _koha_modify_biblio { $biblio->{'abstract'}, $biblio->{'biblionumber'} ) if $biblio->{'biblionumber'}; + my $cache = Koha::Cache::Memory::Lite->get_instance(); + $cache->clear_from_cache("FrameworkCode-" . $biblio->{biblionumber}); + if ( $dbh->errstr || !$biblio->{'biblionumber'} ) { $error .= "ERROR in _koha_modify_biblio $query" . $dbh->errstr; warn $error; diff --git a/Koha/Item.pm b/Koha/Item.pm index 04428facdd..69a0186781 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -1049,16 +1049,14 @@ This is meant to be used for display purpose only. sub columns_to_str { my ( $self ) = @_; + my $frameworkcode = C4::Biblio::GetFrameworkCode($self->biblionumber); + my $tagslib = C4::Biblio::GetMarcStructure( 1, $frameworkcode, { unsafe => 1 } ); + my $mss = C4::Biblio::GetMarcSubfieldStructure( $frameworkcode, { unsafe => 1 } ); - my $frameworkcode = $self->biblio->frameworkcode; - my $tagslib = C4::Biblio::GetMarcStructure(1, $frameworkcode); my ( $itemtagfield, $itemtagsubfield) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" ); - my $columns_info = $self->_result->result_source->columns_info; - - my $mss = C4::Biblio::GetMarcSubfieldStructure( $frameworkcode, { unsafe => 1 } ); my $values = {}; - for my $column ( keys %$columns_info ) { + for my $column ( @{$self->_columns}) { next if $column eq 'more_subfields_xml'; @@ -2097,10 +2095,8 @@ or staff client strings. sub strings_map { my ( $self, $params ) = @_; - - my $columns_info = $self->_result->result_source->columns_info; - my $frameworkcode = $self->biblio->frameworkcode; - my $tagslib = C4::Biblio::GetMarcStructure( 1, $frameworkcode ); + my $frameworkcode = C4::Biblio::GetFrameworkCode($self->biblionumber); + my $tagslib = C4::Biblio::GetMarcStructure( 1, $frameworkcode, { unsafe => 1 } ); my $mss = C4::Biblio::GetMarcSubfieldStructure( $frameworkcode, { unsafe => 1 } ); my ( $itemtagfield, $itemtagsubfield ) = C4::Biblio::GetMarcFromKohaField("items.itemnumber"); @@ -2115,7 +2111,7 @@ sub strings_map { # Handle not null and default values for integers and dates my $strings = {}; - foreach my $col ( keys %{$columns_info} ) { + foreach my $col ( @{$self->_columns} ) { # By now, we are done with known columns, now check the framework for mappings my $field = $self->_result->result_source->name . '.' . $col; diff --git a/t/db_dependent/Biblio.t b/t/db_dependent/Biblio.t index c176885efe..1edf349e65 100755 --- a/t/db_dependent/Biblio.t +++ b/t/db_dependent/Biblio.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 17; +use Test::More tests => 18; use Test::MockModule; use Test::Warn; use List::MoreUtils qw( uniq ); @@ -920,6 +920,27 @@ subtest 'record_schema test' => sub { }; +subtest 'GetFrameworkCode' => sub { + plan tests => 4; + + my $biblio = $builder->build_sample_biblio({ frameworkcode => 'OBP' }); + + is(GetFrameworkCode($biblio->biblionumber), 'OBP', 'GetFrameworkCode returns correct frameworkcode'); + + my $cache = Koha::Cache::Memory::Lite->get_instance(); + my $cache_key = "FrameworkCode-" . $biblio->biblionumber; + my $frameworkcode = $cache->get_from_cache($cache_key); + is($frameworkcode, 'OBP', 'Cache has been set in GetFrameworkCode'); + + # Set new value directly in cache to make sure it's actually being used + $cache->set_in_cache($cache_key, 'OD'); + is(GetFrameworkCode($biblio->biblionumber), 'OD', 'GetFrameworkCode returns correct frameworkcode, using cache'); + + # Test cache invalidation + ModBiblio($biblio->metadata->record, $biblio->biblionumber, 'OGR'); + is(GetFrameworkCode($biblio->biblionumber), 'OGR', 'GetFrameworkCode returns correct frameworkcode after setting a new one though ModBiblio'); + +}; # Cleanup Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );