@@ -, +, @@ --- C4/Context.pm | 106 ++++------------------------------------------------------ 1 file changed, 7 insertions(+), 99 deletions(-) --- a/C4/Context.pm +++ a/C4/Context.pm @@ -103,6 +103,7 @@ use XML::Simple; use C4::Boolean; use C4::Debug; use C4::Log qw/ logaction /; +use Koha::Cache; use POSIX (); use DateTime::TimeZone; use Module::Load::Conditional qw(can_load); @@ -518,67 +519,6 @@ sub ModZebrations { return _common_config($_[1],'serverinfo'); } -=head2 _cache_preference($variable, [$value]) - - _cache_preference('opacusercss', $css); - my $val = _cache_preference('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 ) { - # This means we want to fetch from the cache - 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 ) { - # Set something in the cache - 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 ]; - } - } else { - confess "Invalid arguments to _cache_preference: " . join(', ', @_); - } -} - =head2 preference $sys_preference = C4::Context->preference('some_variable'); @@ -595,11 +535,12 @@ with this method. =cut +my $syspref_cache = Koha::Cache->get_instance(); sub preference { my $self = shift; my $var = shift; # The system preference to return - my $cached_var = _cache_preference($var); + my $cached_var = $syspref_cache->get_from_cache("syspref_$var"); return $cached_var if defined $cached_var; my $dbh = C4::Context->dbh or return 0; @@ -618,7 +559,7 @@ sub preference { $value = $dbh->selectrow_array( $sql, {}, lc $var ); } - _cache_preference($var, $value); + $syspref_cache->set_in_cache("syspref_$var", $value); return $value; } @@ -629,35 +570,6 @@ sub boolean_preference { return defined($it)? C4::Boolean::true_p($it): undef; } -=head2 enable_syspref_cache - - C4::Context->enable_syspref_cache(); - -Enable the in-memory syspref cache used by C4::Context. This is the -default behavior. - -=cut - -sub enable_syspref_cache { - my ($self) = @_; - $use_syspref_cache = 1; -} - -=head2 disable_syspref_cache - - C4::Context->disable_syspref_cache(); - -Disable the in-memory syspref cache used by C4::Context. This should be -used with Plack and other persistent environments. - -=cut - -sub disable_syspref_cache { - my ($self) = @_; - $use_syspref_cache = 0; - $self->clear_syspref_cache(); -} - =head2 clear_syspref_cache C4::Context->clear_syspref_cache(); @@ -669,11 +581,7 @@ will not be seen by this process. =cut 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; + $syspref_cache->flush_all; } =head2 set_preference @@ -719,7 +627,7 @@ ON DUPLICATE KEY UPDATE value = VALUES(value)'; } my $sth = $dbh->prepare($query); if ( $sth->execute(@params) ) { - _cache_preference($var, $value); + $syspref_cache->set_in_cache("syspref_$var", $value); logaction( 'SYSTEMPREFERENCE', $logtype, undef, $var . " | " . $value ); } $sth->finish; @@ -744,7 +652,7 @@ sub delete_preference { my $sth = $dbh->prepare("DELETE FROM systempreferences WHERE variable=?"); my $res = $sth->execute($var); if ($res) { - _cache_preference($var, undef); + $syspref_cache->clear_from_cache("syspref_$var"); logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, $var . " | " . ( $val // 'undefined' ) ); return 1; --