[Koha-patches] [PATCH] bug_15562: Sysprefs cache is object property now

Srdjan srdjan at catalyst.net.nz
Mon Mar 21 06:21:04 CET 2016


https://bugs.koha-community.org/show_bug.cgi?id=15562
---
 C4/Context.pm             | 36 +++++++++++++++++++-----------------
 t/db_dependent/sysprefs.t | 12 +++++++++++-
 2 files changed, 30 insertions(+), 18 deletions(-)

diff --git a/C4/Context.pm b/C4/Context.pm
index bae5f12..3f94baf 100644
--- a/C4/Context.pm
+++ b/C4/Context.pm
@@ -370,6 +370,7 @@ sub new {
     $self->{"activeuser"} = undef;        # current active user
     $self->{"shelves"} = undef;
     $self->{tz} = undef; # local timezone object
+    $self->{sysprefs} = {};
 
     bless $self, $class;
     $self->{db_driver} = db_scheme2dbi($self->config('db_scheme'));  # cache database driver
@@ -505,7 +506,6 @@ with this method.
 =cut
 
 my $syspref_cache = Koha::Cache->get_instance();
-my %syspref_L1_cache;
 my $use_syspref_cache = 1;
 sub preference {
     my $self = shift;
@@ -513,10 +513,9 @@ sub preference {
 
     $var = lc $var;
 
-    # Return the value if the var has already been accessed
-    if ($use_syspref_cache && exists $syspref_L1_cache{$var}) {
-        return $syspref_L1_cache{$var};
-    }
+    $self = $context unless ref $self;
+
+    return $self->{sysprefs}{$var} if $use_syspref_cache && $self && exists $self->{sysprefs}{$var};
 
     my $cached_var = $use_syspref_cache
         ? $syspref_cache->get_from_cache("syspref_$var")
@@ -526,15 +525,13 @@ sub preference {
     my $value;
     if ( defined $ENV{"OVERRIDE_SYSPREF_$var"} ) {
         $value = $ENV{"OVERRIDE_SYSPREF_$var"};
-    } else {
-        my $syspref;
-        eval { $syspref = Koha::Config::SysPrefs->find( lc $var ) };
-        $value = $syspref ? $syspref->value() : undef;
+    } elsif ( my $syspref = Koha::Config::SysPrefs->find( $var ) ) {
+        $value = $syspref->value();
     }
 
     if ( $use_syspref_cache ) {
         $syspref_cache->set_in_cache("syspref_$var", $value);
-        $syspref_L1_cache{$var} = $value;
+        $self->{sysprefs}{$var} = $value if $self;
     }
     return $value;
 }
@@ -588,13 +585,14 @@ will not be seen by this process.
 =cut
 
 sub clear_syspref_cache {
+    my ($self) = @_;
+
+    $self = $context unless ref $self;
+
     return unless $use_syspref_cache;
-    $syspref_cache->flush_all;
-    clear_syspref_L1_cache()
-}
 
-sub clear_syspref_L1_cache {
-    %syspref_L1_cache = ();
+    $syspref_cache->flush_all;
+    $self->{sysprefs} = {} if $self;
 }
 
 =head2 set_preference
@@ -613,6 +611,8 @@ sub set_preference {
 
     $variable = lc $variable;
 
+    $self = $context unless ref $self;
+
     my $syspref = Koha::Config::SysPrefs->find($variable);
     $type =
         $type    ? $type
@@ -647,7 +647,7 @@ sub set_preference {
 
     if ( $use_syspref_cache ) {
         $syspref_cache->set_in_cache( "syspref_$variable", $value );
-        $syspref_L1_cache{$variable} = $value;
+        $self->{sysprefs}{$variable} = $value if $self;
     }
 
     return $syspref;
@@ -666,10 +666,12 @@ was no syspref of the name.
 sub delete_preference {
     my ( $self, $var ) = @_;
 
+    $self = $context unless ref $self;
+
     if ( Koha::Config::SysPrefs->find( $var )->delete ) {
         if ( $use_syspref_cache ) {
             $syspref_cache->clear_from_cache("syspref_$var");
-            delete $syspref_L1_cache{$var};
+            delete $self->{sysprefs}{$var} if $self;
         }
 
         return 1;
diff --git a/t/db_dependent/sysprefs.t b/t/db_dependent/sysprefs.t
index 340e89a..d18a3f1 100755
--- a/t/db_dependent/sysprefs.t
+++ b/t/db_dependent/sysprefs.t
@@ -19,7 +19,7 @@
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-use Test::More tests => 8;
+use Test::More tests => 11;
 use C4::Context;
 
 # Start transaction
@@ -60,4 +60,14 @@ is(C4::Context->preference('testpreference'), 'def', 'caching preferences');
 C4::Context->clear_syspref_cache();
 is(C4::Context->preference('testpreference'), undef, 'clearing preference cache');
 
+delete $ENV{OVERRIDE_SYSPREF_opacheader};
+
+my $context1 = C4::Context->new();
+is( $context1->preference('opacheader'), $opacheader, 'context1 "opacheader"');
+
+my $context2 = C4::Context->new();
+$context2->set_preference( 'opacheader', $newopacheader );
+is( $context1->preference('opacheader'), $opacheader, 'context1 "opacheader"');
+is( $context2->preference('opacheader'), $newopacheader, 'context2 "opacheader"');
+
 $dbh->rollback;
-- 
1.9.1


More information about the Koha-patches mailing list