I believe the problem of the XML file being read for each "use" statement is primarily attributable to the bad way Context uses an "import" function that then calls its own constructor (each time!).
This also prevents us from doing something intelligent like:
Each time a Perl script or a module has this statement:
use C4::Context
a default context was created and XML config file was loaded.
A syspref hash caches syspref already read for the whole
session. A better approach would cash syspref for the
whole application: mod_perl required (or memcached)
---
C4/Context.pm | 8 ++++++--
1 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/C4/Context.pm b/C4/Context.pm
index e81fd95..ac2255f 100644
--- a/C4/Context.pm
+++ b/C4/Context.pm
@@ -248,11 +248,10 @@ sub db_scheme2dbi {
sub import {
my $package = shift;
my $conf_fname = shift; # Config file name
- my $context;
# Create a new context from the given config file name, if
# any, then set it as the current context.
- $context = new C4::Context($conf_fname);
+ $context = new C4::Context($conf_fname) unless $context;
return undef if !defined($context);
$context->set_context;
}
@@ -445,10 +444,14 @@ variable is not set, or in case of error, returns the undefined value.
# 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.
+
+my %syspref;
+
sub preference
{
my $self = shift;
my $var = shift; # The system preference to return
+ return $syspref{$var} if $syspref{$var};
my $retval; # Return value
my $dbh = C4::Context->dbh or return 0;
# Look up systempreferences.variable==$var
@@ -458,6 +461,7 @@ sub preference
WHERE variable='$var'
LIMIT 1
EOT
+ $syspref{$var} = $retval;
return $retval;
}