From 759c5c4298a4d04ab2aedbd730b7431ec22f2cd6 Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Wed, 26 Mar 2014 19:07:04 +1300 Subject: [PATCH] Bug 11998 - timeout for syspref caching This adds a 30 second timeout for syspref caching so that when run under plack, you don't end up with different workers having different ideas of what the sysprefs are for too long. Signed-off-by: Brendan Gallagher --- C4/Context.pm | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/C4/Context.pm b/C4/Context.pm index cc93af4..10a1a33 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -534,18 +534,22 @@ with this method. =cut -# FIXME: running this under mod_perl will require a means of -# flushing the caching mechanism. - -my %sysprefs; +my %syspref_cache; my $use_syspref_cache = 1; sub preference { my $self = shift; my $var = shift; # The system preference to return - if ($use_syspref_cache && exists $sysprefs{lc $var}) { - return $sysprefs{lc $var}; + # We cache for 30 seconds, after that sysprefs are expired from the cache. + # This is for persistent environments like Plack. + my $now = time; + my $cache_oldest = $now - 30; + if ($use_syspref_cache && exists $syspref_cache{lc $var}) { + # The cache contains an arrayref, first entry is the time it was + # stored, the second is the value. + my $cached_value = $syspref_cache{lc $var}; + return $cached_value->[1] if ($cached_value->[0] > $cache_oldest); } my $dbh = C4::Context->dbh or return 0; @@ -564,7 +568,7 @@ sub preference { $value = $dbh->selectrow_array( $sql, {}, lc $var ); } - $sysprefs{lc $var} = $value; + $syspref_cache{lc $var} = [$now, $value]; return $value; } @@ -615,7 +619,7 @@ will not be seen by this process. =cut sub clear_syspref_cache { - %sysprefs = (); + %syspref_cache= (); } =head2 set_preference @@ -646,7 +650,7 @@ sub set_preference { " ); if($sth->execute( $var, $value )) { - $sysprefs{$var} = $value; + $syspref_cache{$var} = [time, $value]; } $sth->finish; } -- 2.1.0