From 237a3222b122d1430aa2bff948b417758db7eeb2 Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Tue, 1 Apr 2014 16:16:29 +1300 Subject: [PATCH] Bug 11998 - sysprefs in memcached The caching mechanism has been abstracted a little bit, and now supports memcached for storing sysprefs. These should be correctly stored and cleared so caching will work between processes. --- C4/Context.pm | 82 +++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 16 deletions(-) diff --git a/C4/Context.pm b/C4/Context.pm index 5249cab..f274be7 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -518,6 +518,63 @@ sub ModZebrations { return _common_config($_[1],'serverinfo'); } +=head2 _cache_preference($variable, [$value]) + + _cache_preferences('opacusercss', $css); + my $val = _cach_preferences('opacusercss'); + +An interface into the syspref caching. One argument to get a value, two to set +it. The single argument version will return C if the cache doesn't +contain the value. If the second argument is C, then the cache will be +cleared of that value. + +The cache timeout length is 30 seconds when memcache is not in use. When +memcache is in use, it uses the default cache time for that. + +=cut + +my %syspref_cache; +my $use_syspref_cache = 1; + +sub _cache_preference { + return undef unless $use_syspref_cache; + my $memcached = memcached(); + my $memcached_prefix = "_syspref_"; + if ( scalar(@_) == 1 ) { + my ($var) = @_; + if ($memcached) { + my $val = $memcached->get( $memcached_prefix . lc($var) ); + return $val; + } + else { + my $now = time; + my $cache_oldest = $now - 30; + return undef unless exists $syspref_cache{ lc $var }; + my $val = $syspref_cache{ lc $var }; + return undef unless $val->[0] > $cache_oldest; + return $val->[1]; + } + } + elsif ( scalar(@_) == 2 ) { + my ( $var, $val ) = @_; + if ($memcached) { + if ( !defined($val) ) { + $memcached->delete( $memcached_prefix . lc($var) ); + } + else { + $memcached->set( $memcached_prefix . lc($var), $val ); + } + } + else { + if ( !defined($val) ) { + delete $syspref_cache{ lc $var }; + return; + } + $syspref_cache{ lc $var } = [ time, $val ]; + } + } +} + =head2 preference $sys_preference = C4::Context->preference('some_variable'); @@ -534,23 +591,12 @@ with this method. =cut -my %syspref_cache; -my $use_syspref_cache = 1; - sub preference { my $self = shift; my $var = shift; # The system preference to return - # 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 $cached_var = _cache_preference($var); + return $cached_var if defined $cached_var; my $dbh = C4::Context->dbh or return 0; @@ -568,7 +614,7 @@ sub preference { $value = $dbh->selectrow_array( $sql, {}, lc $var ); } - $syspref_cache{lc $var} = [$now, $value]; + _cache_preference($var, $value); return $value; } @@ -620,6 +666,10 @@ will not be seen by this process. sub clear_syspref_cache { %syspref_cache= (); + # It doesn't seem to be possible to list all the keys via Cache::Memcached + # so we'll clear the whole cache. + my $memcached = memcached(); + $memcached->flush_all if $memcached; } =head2 set_preference @@ -665,7 +715,7 @@ ON DUPLICATE KEY UPDATE value = VALUES(value)'; } my $sth = $dbh->prepare($query); if ( $sth->execute(@params) ) { - $syspref_cache{$var} = [ time, $value ]; + _cache_preference($var, $value); logaction( 'SYSTEMPREFERENCE', $logtype, undef, $var . " | " . $value ); } $sth->finish; @@ -690,7 +740,7 @@ sub delete_preference { my $sth = $dbh->prepare("DELETE FROM systempreferences WHERE variable=?"); my $res = $sth->execute($var); if ($res) { - delete $syspref_cache{$var}; + C4::Context->set_preference($var, undef); logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $var . " | " . ( $var // 'undefined' ) ); return 1; -- 1.8.3.2