From 5933b324a7865396048b4b54e0ed4c55c32e22e0 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 c5f92ee..0ac1739 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -533,18 +533,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; @@ -563,7 +567,7 @@ sub preference { $value = $dbh->selectrow_array( $sql, {}, lc $var ); } - $sysprefs{lc $var} = $value; + $syspref_cache{lc $var} = [$now, $value]; return $value; } @@ -614,7 +618,7 @@ will not be seen by this process. =cut sub clear_syspref_cache { - %sysprefs = (); + %syspref_cache= (); } =head2 set_preference @@ -645,7 +649,7 @@ sub set_preference { " ); if($sth->execute( $var, $value )) { - $sysprefs{$var} = $value; + $syspref_cache{$var} = [time, $value]; } $sth->finish; } -- 1.7.10.4