Bug 8089: Cache sysprefs using Koha::Cache
authorJared Camins-Esakov <jcamins@cpbibliography.com>
Tue, 15 May 2012 11:14:01 +0000 (07:14 -0400)
committerPaul Poulain <paul.poulain@biblibre.com>
Fri, 7 Sep 2012 14:28:31 +0000 (16:28 +0200)
Cache sysprefs using Koha::Cache in a way that is safe even for
caching-related sysprefs. This lays the groundwork for removing
caching configuration from the httpd.conf and configuring it
using sysprefs.

Signed-off-by: Chris Cormack <chris@bigballofwax.co.nz>
C4/Context.pm

index 5e9feae..0a56aa8 100644 (file)
@@ -487,11 +487,20 @@ my %sysprefs;
 sub preference {
     my $self = shift;
     my $var  = lc(shift);                          # The system preference to return
+    my $cache;
 
     if (exists $sysprefs{$var}) {
         return $sysprefs{$var};
     }
 
+    if (Koha::Cache->is_cache_active()) {
+        $cache = Koha::Cache->new();
+        if (defined $cache) {
+            $sysprefs{$var} = $cache->get_from_cache("syspref:$var");
+            return $sysprefs{$var} if (defined $sysprefs{$var});
+        }
+    }
+
     my $dbh  = C4::Context->dbh or return 0;
 
     # Look up systempreferences.variable==$var
@@ -502,6 +511,9 @@ sub preference {
         LIMIT    1
 END_SQL
     $sysprefs{$var} = $dbh->selectrow_array( $sql, {}, $var );
+    if (Koha::Cache->is_cache_active() && defined $cache) {
+        $cache->set_in_cache("syspref:$var");
+    }
     return $sysprefs{$var};
 }
 
@@ -524,6 +536,10 @@ will not be seen by this process.
 
 sub clear_syspref_cache {
     %sysprefs = ();
+    if (Koha::Cache->is_cache_active()) {
+        my $cache = Koha::Cache->new();
+        $cache->flush_all() if defined $cache; # Sorry, this is unpleasant
+    }
 }
 
 =head2 set_preference
@@ -554,6 +570,10 @@ sub set_preference {
     " );
 
     if($sth->execute( $var, $value )) {
+        if (Koha::Cache->is_cache_active()) {
+            my $cache = Koha::Cache->new();
+            $cache->set_in_cache("syspref:$var", $value) if defined $cache;
+        }
         $sysprefs{$var} = $value;
     }
     $sth->finish;