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

Srdjan srdjan at catalyst.net.nz
Fri Feb 12 05:57:36 CET 2016


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

diff --git a/C4/Context.pm b/C4/Context.pm
index 341438a..1dcdce4 100644
--- a/C4/Context.pm
+++ b/C4/Context.pm
@@ -368,6 +368,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,29 +506,27 @@ with this method.
 # FIXME: running this under mod_perl will require a means of
 # flushing the caching mechanism.
 
-my %sysprefs;
 my $use_syspref_cache = 1;
 
 sub preference {
     my $self = shift;
     my $var  = shift;    # The system preference to return
+    my $lc_var = lc $var;
 
-    if ($use_syspref_cache && exists $sysprefs{lc $var}) {
-        return $sysprefs{lc $var};
-    }
+    $self = $context unless ref $self;
+
+    return $self->{sysprefs}{$lc_var} if $use_syspref_cache && $self && exists $self->{sysprefs}{$lc_var};
 
     my $dbh  = C4::Context->dbh or return 0;
 
     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( $lc_var ) ) {
+        $value = $syspref->value();
     }
 
-    $sysprefs{lc $var} = $value;
+    $self->{sysprefs}{$lc_var} = $value if $use_syspref_cache && $self;
     return $value;
 }
 
@@ -578,7 +577,11 @@ will not be seen by this process.
 =cut
 
 sub clear_syspref_cache {
-    %sysprefs = ();
+    my ($self) = @_;
+
+    $self = $context unless ref $self;
+
+    $self->{sysprefs} = {} if $self;
 }
 
 =head2 set_preference
@@ -595,6 +598,8 @@ sub set_preference {
     my $var = lc(shift);
     my $value = shift;
 
+    $self = $context unless ref $self;
+
     my $syspref = Koha::Config::SysPrefs->find( $var );
     my $type = $syspref ? $syspref->type() : undef;
 
@@ -612,9 +617,7 @@ sub set_preference {
         $syspref = Koha::Config::SysPref->new( { variable => $var, value => $value } )->store();
     }
 
-    if ($syspref) {
-        $sysprefs{$var} = $value;
-    }
+    $self->{sysprefs}{$var} = $value if $use_syspref_cache && $self;
 }
 
 =head2 Zconn
diff --git a/t/db_dependent/sysprefs.t b/t/db_dependent/sysprefs.t
index c678d24..f24390c 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 => 5;
+use Test::More tests => 8;
 use C4::Context;
 
 # Start transaction
@@ -48,3 +48,15 @@ is( C4::Context->preference('IDoNotExist'), undef, 'Get a non-existent system pr
 
 C4::Context->set_preference( 'IDoNotExist', 'NonExistent' );
 is( C4::Context->preference('IDoNotExist'), 'NonExistent', 'Test creation of non-existent system preference' );
+
+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