# flushing the caching mechanism.
my %sysprefs;
+my $use_syspref_cache = 1;
sub preference {
my $self = shift;
my $var = lc(shift); # The system preference to return
- if (exists $sysprefs{$var}) {
+ if ($use_syspref_cache && exists $sysprefs{$var}) {
return $sysprefs{$var};
}
return defined($it)? C4::Boolean::true_p($it): undef;
}
+=head2 enable_syspref_cache
+
+ C4::Context->enable_syspref_cache();
+
+Enable the in-memory syspref cache used by C4::Context. This is the
+default behavior.
+
+=cut
+
+sub enable_syspref_cache {
+ my ($self) = @_;
+ $use_syspref_cache = 1;
+}
+
+=head2 disable_syspref_cache
+
+ C4::Context->disable_syspref_cache();
+
+Disable the in-memory syspref cache used by C4::Context. This should be
+used with Plack and other persistent environments.
+
+=cut
+
+sub disable_syspref_cache {
+ my ($self) = @_;
+ $use_syspref_cache = 0;
+ $self->clear_syspref_cache();
+}
+
=head2 clear_syspref_cache
C4::Context->clear_syspref_cache();
use warnings;
use Test::More;
+use Test::MockModule;
use vars qw($debug $koha $dbh $config $ret);
BEGIN {
}
ok($config = $koha->{config}, 'Getting $koha->{config} ');
+diag "Testing syspref caching.";
+
+my $dbh = C4::Context->dbh;
+$dbh->disconnect;
+
+my $module = new Test::MockModule('C4::Context');
+$module->mock(
+ '_new_dbh',
+ sub {
+ my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
+ || die "Cannot create handle: $DBI::errstr\n";
+ return $dbh;
+ }
+);
+
+my $history;
+$dbh = C4::Context->dbh;
+
+$dbh->{mock_add_resultset} = [ ['value'], ['thing1'] ];
+$dbh->{mock_add_resultset} = [ ['value'], ['thing2'] ];
+$dbh->{mock_add_resultset} = [ ['value'], ['thing3'] ];
+$dbh->{mock_add_resultset} = [ ['value'], ['thing4'] ];
+
+is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
+$history = $dbh->{mock_all_history};
+is(scalar(@{$history}), 1, 'Retrieved syspref from database');
+
+$dbh->{mock_clear_history} = 1;
+is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
+$history = $dbh->{mock_all_history};
+is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
+
+C4::Context->disable_syspref_cache();
+is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
+$history = $dbh->{mock_all_history};
+is(scalar(@{$history}), 1, 'Retrieved syspref from database');
+
+$dbh->{mock_clear_history} = 1;
+is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
+$history = $dbh->{mock_all_history};
+is(scalar(@{$history}), 1, 'Retrieved syspref from database');
+
+C4::Context->enable_syspref_cache();
+$dbh->{mock_clear_history} = 1;
+is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
+$history = $dbh->{mock_all_history};
+is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
+
+C4::Context->clear_syspref_cache();
+$dbh->{mock_clear_history} = 1;
+is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
+$history = $dbh->{mock_all_history};
+is(scalar(@{$history}), 1, 'Retrieved syspref from database');
+
+$dbh->{mock_clear_history} = 1;
+is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
+$history = $dbh->{mock_all_history};
+is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
+
done_testing();
1;