From f1a6c4cff5d5f69bcd5c62a8757aff5dafb03df6 Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Thu, 10 Apr 2014 17:11:50 +1200 Subject: [PATCH] Bug 11842 - fix framework caching with memcache/plack This changes the existing framework caching, which was using memoisation if memcached was available, and memory in all cases, to use the Koha::Cache system. This uses memcache if possible, and in-memory otherwise. However it also clears the cache when the framework updates, making sure that the changed version will be picked up. Note that the in-memory cache clears itself after 10 seconds, so that if memcached isn't available, this is the longest that old versions will hang around. Test plan: * work through http://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=11842#c0 and make sure that the erronious result doesn't occur. Note: * The patch on bug 12041 is required for this to work. Signed-off-by: Brendan Gallagher --- C4/Biblio.pm | 32 ++++++++------------------------ admin/biblio_framework.pl | 14 ++++++++++---- admin/koha2marclinks.pl | 4 ++++ admin/marc_subfields_structure.pl | 6 ++++++ admin/marctagstructure.pl | 8 ++++++++ 5 files changed, 36 insertions(+), 28 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 0fb453d..d7d3c53 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -38,6 +38,8 @@ use C4::Charset; use C4::Linker; use C4::OAI::Sets; +use Koha::Cache; + use vars qw($VERSION @ISA @EXPORT); BEGIN { @@ -138,16 +140,6 @@ BEGIN { ); } -eval { - if (C4::Context->ismemcached) { - require Memoize::Memcached; - import Memoize::Memcached qw(memoize_memcached); - - memoize_memcached( 'GetMarcStructure', - memcached => C4::Context->memcached); - } -}; - =head1 NAME C4::Biblio - cataloging management functions @@ -1095,24 +1087,17 @@ $frameworkcode : the framework code to read =cut -# cache for results of GetMarcStructure -- needed -# for batch jobs -our $marc_structure_cache; - sub GetMarcStructure { my ( $forlibrarian, $frameworkcode ) = @_; my $dbh = C4::Context->dbh; $frameworkcode = "" unless $frameworkcode; - if ( defined $marc_structure_cache and exists $marc_structure_cache->{$forlibrarian}->{$frameworkcode} ) { - return $marc_structure_cache->{$forlibrarian}->{$frameworkcode}; - } + $forlibrarian = $forlibrarian ? 1 : 0; + my $cache = Koha::Cache->get_instance(); + my $cache_key = "MarcStructure-$forlibrarian-$frameworkcode"; + my $cached = $cache->get_from_cache($cache_key); + return $cached if $cached; - # my $sth = $dbh->prepare( - # "SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?"); - # $sth->execute($frameworkcode); - # my ($total) = $sth->fetchrow; - # $frameworkcode = "" unless ( $total > 0 ); my $sth = $dbh->prepare( "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable FROM marc_tag_structure @@ -1174,8 +1159,7 @@ sub GetMarcStructure { $res->{$tag}->{$subfield}->{maxlength} = $maxlength; } - $marc_structure_cache->{$forlibrarian}->{$frameworkcode} = $res; - + $cache->set_in_cache($cache_key, $res); return $res; } diff --git a/admin/biblio_framework.pl b/admin/biblio_framework.pl index 583bc50..dfe1a91 100755 --- a/admin/biblio_framework.pl +++ b/admin/biblio_framework.pl @@ -27,6 +27,7 @@ use CGI; use C4::Context; use C4::Auth; use C4::Output; +use Koha::Cache; sub StringSearch { my $dbh = C4::Context->dbh; @@ -41,6 +42,7 @@ my $frameworkcode = $input->param('frameworkcode') || ''; my $offset = $input->param('offset') || 0; my $op = $input->param('op') || ''; my $pagesize = 20; +my $cache = Koha::Cache->get_instance(); my ($template, $borrowernumber, $cookie) = get_template_and_user({template_name => "admin/biblio_framework.tmpl", @@ -74,15 +76,17 @@ if ($op eq 'add_form') { ################## ADD_VALIDATE ################################## # called by add_form, used to insert/modify data in DB } elsif ($op eq 'add_validate') { - my $dbh = C4::Context->dbh; - if($input->param('frameworktext') and $input->param('frameworkcode')){ + my $dbh = C4::Context->dbh; + if ( $input->param('frameworktext') and $frameworkcode ) { if ($input->param('modif')) { my $sth=$dbh->prepare("UPDATE biblio_framework SET frameworktext=? WHERE frameworkcode=?"); - $sth->execute($input->param('frameworktext'),$input->param('frameworkcode')); + $sth->execute( $input->param('frameworktext'), $frameworkcode ); } else { my $sth=$dbh->prepare("INSERT into biblio_framework (frameworkcode,frameworktext) values (?,?)"); - $sth->execute($input->param('frameworkcode'),$input->param('frameworktext')); + $sth->execute( $frameworkcode, $input->param('frameworktext') ); } + $cache->clear_from_cache("MarcStructure-0-$frameworkcode"); + $cache->clear_from_cache("MarcStructure-1-$frameworkcode"); } print $input->redirect($script_name); # FIXME: unnecessary redirect exit; @@ -115,6 +119,8 @@ if ($op eq 'add_form') { $sth->execute($frameworkcode); $sth=$dbh->prepare("delete from biblio_framework where frameworkcode=?"); $sth->execute($frameworkcode); + $cache->clear_from_cache("MarcStructure-0-$frameworkcode"); + $cache->clear_from_cache("MarcStructure-1-$frameworkcode"); } print $input->redirect($script_name); # FIXME: unnecessary redirect exit; diff --git a/admin/koha2marclinks.pl b/admin/koha2marclinks.pl index fc320ad..8a0dcfe 100755 --- a/admin/koha2marclinks.pl +++ b/admin/koha2marclinks.pl @@ -59,6 +59,7 @@ else { } my $dbh = C4::Context->dbh; +my $cache = Koha::Cache->get_instance(); ################## ADD_FORM ################################## # called by default. Used to create form to add or modify a record @@ -116,6 +117,9 @@ elsif ( $op eq 'add_validate' ) { $dbh->do( "update marc_subfield_structure set kohafield='$tablename.$kohafield' where tagfield='$temp[0]' and tagsubfield='$temp[1]'" ); + # We could get a list of all frameworks and do them one-by-one, or zap + # everything. + $cache->flush_all(); print "Content-Type: text/html\n\n"; exit; diff --git a/admin/marc_subfields_structure.pl b/admin/marc_subfields_structure.pl index 0589968..372aeac 100755 --- a/admin/marc_subfields_structure.pl +++ b/admin/marc_subfields_structure.pl @@ -75,6 +75,7 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user( debug => 1, } ); +my $cache = Koha::Cache->get_instance(); my $op = $input->param('op'); $tagfield =~ s/\,//g; @@ -483,6 +484,9 @@ elsif ( $op eq 'add_validate' ) { } $sth_insert->finish; $sth_update->finish; + $cache->clear_from_cache("MarcStructure-0-$frameworkcode"); + $cache->clear_from_cache("MarcStructure-1-$frameworkcode"); + print "Content-Type: text/html\n\n"; exit; @@ -524,6 +528,8 @@ elsif ( $op eq 'delete_confirmed' ) { $sth->execute( $tagfield, $tagsubfield, $frameworkcode ); $sth->finish; } + $cache->clear_from_cache("MarcStructure-0-$frameworkcode"); + $cache->clear_from_cache("MarcStructure-1-$frameworkcode"); print "Content-Type: text/html\n\n"; exit; diff --git a/admin/marctagstructure.pl b/admin/marctagstructure.pl index d3b227d..bd3cd1a 100755 --- a/admin/marctagstructure.pl +++ b/admin/marctagstructure.pl @@ -27,6 +27,7 @@ use C4::Context; use C4::Output; use C4::Context; +use Koha::Cache; # retrieve parameters my $input = new CGI; @@ -45,6 +46,7 @@ my $pagesize = 20; my $script_name = "/cgi-bin/koha/admin/marctagstructure.pl"; my $dbh = C4::Context->dbh; +my $cache = Koha::Cache->get_instance(); # open template my ($template, $loggedinuser, $cookie) @@ -167,6 +169,8 @@ if ($op eq 'add_form') { $frameworkcode ); } + $cache->clear_from_cache("MarcStructure-0-$frameworkcode"); + $cache->clear_from_cache("MarcStructure-1-$frameworkcode"); } print $input->redirect("/cgi-bin/koha/admin/marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode"); exit; @@ -191,6 +195,8 @@ if ($op eq 'add_form') { my $sth2 = $dbh->prepare("DELETE FROM marc_subfield_structure WHERE tagfield=? AND frameworkcode=?"); $sth1->execute($searchfield, $frameworkcode); $sth2->execute($searchfield, $frameworkcode); + $cache->clear_from_cache("MarcStructure-0-$frameworkcode"); + $cache->clear_from_cache("MarcStructure-1-$frameworkcode"); } $template->param( searchfield => $searchfield, @@ -354,5 +360,7 @@ sub duplicate_framework { while ( my ($frameworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso,$hidden) = $sth->fetchrow) { $sth_insert->execute($newframeworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso, $hidden); } + $cache->clear_from_cache("MarcStructure-0-$newframeworkcode"); + $cache->clear_from_cache("MarcStructure-1-$newframeworkcode"); } -- 1.7.10.4