Cache the FS::Conf info per page load, for performance.
[freeside.git] / FS / FS / Conf.pm
index c4e5af4..855c9f4 100644 (file)
@@ -1,7 +1,7 @@
 package FS::Conf;
 
-use vars qw($base_dir @config_items @base_items @card_types $DEBUG);
 use strict;
+use vars qw($base_dir @config_items @base_items @card_types $DEBUG $conf_cache);
 use Carp;
 use IO::File;
 use File::Basename;
@@ -134,9 +134,13 @@ sub _config {
   foreach my $a (@a) {
     $hashref->{agentnum} = $a;
     foreach my $l (@l) {
-      $hashref->{locale} = $l;
-      $cv = FS::Record::qsearchs('conf', $hashref);
-      return $cv if $cv;
+      my $key = join(':',$name, $a, $l);
+      if (! exists $conf_cache->{$key}){
+        $hashref->{locale} = $l;
+        # $conf_cache is reset in FS::UID during myconnect, so the cache is reset per connection
+        $conf_cache->{$key} = FS::Record::qsearchs('conf', $hashref);
+      }
+      return $conf_cache->{$key} if $conf_cache->{$key};
     }
   }
   return undef;
@@ -362,6 +366,12 @@ sub set {
     $error = $new->insert;
   }
 
+  if (! $error) {
+    # clean the object cache
+    my $key = join(':',$name, $agentnum, $self->{locale});
+    $conf_cache->{ $key } = $new;
+  }
+
   die "error setting configuration value: $error \n"
     if $error;