View | Details | Raw Unified | Return to bug 9005
Collapse All | Expand All

(-)a/C4/Context.pm (-1 / +31 lines)
Lines 523-534 with this method. Link Here
523
# flushing the caching mechanism.
523
# flushing the caching mechanism.
524
524
525
my %sysprefs;
525
my %sysprefs;
526
my $use_syspref_cache = 1;
526
527
527
sub preference {
528
sub preference {
528
    my $self = shift;
529
    my $self = shift;
529
    my $var  = lc(shift);                          # The system preference to return
530
    my $var  = lc(shift);                          # The system preference to return
530
531
531
    if (exists $sysprefs{$var}) {
532
    if ($use_syspref_cache && exists $sysprefs{$var}) {
532
        return $sysprefs{$var};
533
        return $sysprefs{$var};
533
    }
534
    }
534
535
Lines 552-557 sub boolean_preference { Link Here
552
    return defined($it)? C4::Boolean::true_p($it): undef;
553
    return defined($it)? C4::Boolean::true_p($it): undef;
553
}
554
}
554
555
556
=head2 enable_syspref_cache
557
558
  C4::Context->enable_syspref_cache();
559
560
Enable the in-memory syspref cache used by C4::Context. This is the
561
default behavior.
562
563
=cut
564
565
sub enable_syspref_cache {
566
    my ($self) = @_;
567
    $use_syspref_cache = 1;
568
}
569
570
=head2 disable_syspref_cache
571
572
  C4::Context->disable_syspref_cache();
573
574
Disable the in-memory syspref cache used by C4::Context. This should be
575
used with Plack and other persistent environments.
576
577
=cut
578
579
sub disable_syspref_cache {
580
    my ($self) = @_;
581
    $use_syspref_cache = 0;
582
    $self->clear_syspref_cache();
583
}
584
555
=head2 clear_syspref_cache
585
=head2 clear_syspref_cache
556
586
557
  C4::Context->clear_syspref_cache();
587
  C4::Context->clear_syspref_cache();
(-)a/t/db_dependent/Context.t (-1 / +60 lines)
Lines 5-10 use strict; Link Here
5
use warnings;
5
use warnings;
6
6
7
use Test::More;
7
use Test::More;
8
use Test::MockModule;
8
use vars qw($debug $koha $dbh $config $ret);
9
use vars qw($debug $koha $dbh $config $ret);
9
10
10
BEGIN {
11
BEGIN {
Lines 42-47 foreach (grep {defined $koha->{$_}} sort @keys) { Link Here
42
}
43
}
43
ok($config = $koha->{config}, 'Getting $koha->{config} ');
44
ok($config = $koha->{config}, 'Getting $koha->{config} ');
44
45
46
diag "Testing syspref caching.";
47
48
my $dbh = C4::Context->dbh;
49
$dbh->disconnect;
50
51
my $module = new Test::MockModule('C4::Context');
52
$module->mock(
53
    '_new_dbh',
54
    sub {
55
        my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
56
          || die "Cannot create handle: $DBI::errstr\n";
57
        return $dbh;
58
    }
59
);
60
61
my $history;
62
$dbh = C4::Context->dbh;
63
64
$dbh->{mock_add_resultset} = [ ['value'], ['thing1'] ];
65
$dbh->{mock_add_resultset} = [ ['value'], ['thing2'] ];
66
$dbh->{mock_add_resultset} = [ ['value'], ['thing3'] ];
67
$dbh->{mock_add_resultset} = [ ['value'], ['thing4'] ];
68
69
is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
70
$history = $dbh->{mock_all_history};
71
is(scalar(@{$history}), 1, 'Retrieved syspref from database');
72
73
$dbh->{mock_clear_history} = 1;
74
is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
75
$history = $dbh->{mock_all_history};
76
is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
77
78
C4::Context->disable_syspref_cache();
79
is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
80
$history = $dbh->{mock_all_history};
81
is(scalar(@{$history}), 1, 'Retrieved syspref from database');
82
83
$dbh->{mock_clear_history} = 1;
84
is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
85
$history = $dbh->{mock_all_history};
86
is(scalar(@{$history}), 1, 'Retrieved syspref from database');
87
88
C4::Context->enable_syspref_cache();
89
$dbh->{mock_clear_history} = 1;
90
is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
91
$history = $dbh->{mock_all_history};
92
is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
93
94
C4::Context->clear_syspref_cache();
95
$dbh->{mock_clear_history} = 1;
96
is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
97
$history = $dbh->{mock_all_history};
98
is(scalar(@{$history}), 1, 'Retrieved syspref from database');
99
100
$dbh->{mock_clear_history} = 1;
101
is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
102
$history = $dbh->{mock_all_history};
103
is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
104
45
done_testing();
105
done_testing();
46
106
47
1;
107
1;
48
- 

Return to bug 9005