[Koha-patches] systempreferences caching

Clay Fouts clay.fouts at liblime.com
Mon Oct 27 21:21:54 CET 2008


Keeps a cache of variable/value pairs from systempreferences for the
lifetime of the process.

This simply keeps an associative array of variable/value pairs keyed
to the syspref variable's name. If the key exists it returns the
associated value. If not, it fetches it from the database and stores
the value before returning the same to the caller.

Documentation should be updated to warn those using mod_perl that
sysprefs will not update without reloading the Apache server. This
caching stores them for the lifetime of the perl process.

diff --git a/C4/Context.pm b/C4/Context.pm
index 7813f49..08dcae0 100644
--- a/C4/Context.pm
+++ b/C4/Context.pm
@@ -461,12 +461,19 @@ with this method.

 =cut

-# FIXME - The preferences aren't likely to change over the lifetime of
-# the script (and things might break if they did change), so perhaps
-# this function should cache the results it finds.
+# FIXME: running this under mod_perl will require a means of
+# flushing the caching mechanism.
+
+my %sysprefs;
+
 sub preference {
     my $self = shift;
     my $var  = shift;                          # The system
preference to return
+
+    if (exists $sysprefs{$var}) {
+        return $sysprefs{$var};
+    }
+
     my $dbh  = C4::Context->dbh or return 0;

     # Look up systempreferences.variable==$var
@@ -476,8 +483,8 @@ sub preference {
         WHERE    variable=?
         LIMIT    1
 END_SQL
-    my $retval = $dbh->selectrow_array( $sql, {}, $var );
-    return $retval;
+    $sysprefs{$var} = $dbh->selectrow_array( $sql, {}, $var );
+    return $sysprefs{$var};
 }

 sub boolean_preference ($) {



More information about the Koha-patches mailing list