From 0d73904eac286b256e2e72bb6c6560eadc34fcab Mon Sep 17 00:00:00 2001 From: Jesse Weaver Date: Fri, 11 Mar 2016 13:19:49 -0600 Subject: [PATCH] Bug 16140: Only clear L1 cache when needed By storing a modification time in memcached, we can check the upstream cache at the start of every request, and only clear the L1 cache if the upstream has actually been modified. This and the following patch get noticeable performance improvements on top of bug 16044: ~1.2 vs 2.2 seconds for a search on a small dataset. --- C4/Koha.pm | 15 +++++++++++++-- Koha/Cache.pm | 20 ++++++++++++++++++++ debian/templates/plack.psgi | 2 +- misc/plack/koha.psgi | 2 +- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/C4/Koha.pm b/C4/Koha.pm index 7fe07f1..b6a6b34 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -1008,6 +1008,13 @@ C<$opac> If set to a true value, displays OPAC descriptions rather than normal o =cut +sub _AddSelectedAuthVal { + my ( $authorised_values, $selected ) = @_; + foreach my $data ( @$authorised_values ) { + $data->{selected} = $selected eq $data->{authorised_value} ? 1 : 0; + } +} + sub GetAuthorisedValues { my ( $category, $opac ) = @_; @@ -1019,7 +1026,10 @@ sub GetAuthorisedValues { "AuthorisedValues-$category-$opac-$branch_limit"; my $cache = Koha::Cache->get_instance(); my $result = $cache->get_from_cache($cache_key); - return $result if $result; + if ($result) { + _AddSelectedAuthVal( $result, $selected ) if defined $selected; + return $result; + } my @results; my $dbh = C4::Context->dbh; @@ -1060,7 +1070,8 @@ sub GetAuthorisedValues { } $sth->finish; - $cache->set_in_cache( $cache_key, \@results, { deepcopy => 1, expiry => 5 } ); + $cache->set_in_cache( $cache_key, \@results ); + _AddSelectedAuthVal( \@results, $selected ); return \@results; } diff --git a/Koha/Cache.pm b/Koha/Cache.pm index eeb2723..0a196b7 100644 --- a/Koha/Cache.pm +++ b/Koha/Cache.pm @@ -41,6 +41,7 @@ use Carp; use Clone qw( clone ); use Module::Load::Conditional qw(can_load); use Koha::Cache::Object; +use Time::HiRes qw(time); use base qw(Class::Accessor); @@ -48,6 +49,7 @@ __PACKAGE__->mk_ro_accessors( qw( cache memcached_cache fastmmap_cache memory_cache )); our %L1_cache; +our $L1_cache_time; =head2 get_instance @@ -131,6 +133,22 @@ sub new { $class; } +sub flush_L1_if_needed { + my ($self) = @_; + + if ( $self->{cache} ) { + if ( $L1_cache_time ) { + my $upstream_cache_mtime = $self->{cache}->get('upstream_cache_mtime'); + + if ( $upstream_cache_mtime && $upstream_cache_mtime > $L1_cache_time ) { + $self->flush_L1_cache(); + } + } + + $L1_cache_time = time(); + } +} + sub _initialize_memcached { my ($self) = @_; my @servers = @@ -288,6 +306,8 @@ sub set_in_cache { # Set in L1 cache $L1_cache{ $key } = $value; + $self->{$cache}->set( 'upstream_cache_mtime', time() ); + # We consider an expiry of 0 to be inifinite if ( $expiry ) { return $set_sub diff --git a/debian/templates/plack.psgi b/debian/templates/plack.psgi index 995fa72..9c92933 100644 --- a/debian/templates/plack.psgi +++ b/debian/templates/plack.psgi @@ -44,7 +44,7 @@ use CGI qw(-utf8 ); # we will loose -utf8 under plack, otherwise *CGI::new = sub { my $q = $old_new->( @_ ); $CGI::PARAM_UTF8 = 1; - Koha::Cache->flush_L1_cache(); + Koha::Cache->flush_L1_if_needed(); return $q; }; } diff --git a/misc/plack/koha.psgi b/misc/plack/koha.psgi index 7f3ea27..845e47e 100644 --- a/misc/plack/koha.psgi +++ b/misc/plack/koha.psgi @@ -12,7 +12,7 @@ use CGI qw(-utf8 ); # we will lose -utf8 under plack *CGI::new = sub { my $q = $old_new->( @_ ); $CGI::PARAM_UTF8 = 1; - Koha::Cache->flush_L1_cache(); + Koha::Cache->flush_L1_if_needed(); return $q; }; } -- 2.8.0.rc3