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

(-)a/C4/Context.pm (-71 / +9 lines)
Lines 104-109 use XML::Simple; Link Here
104
use C4::Boolean;
104
use C4::Boolean;
105
use C4::Debug;
105
use C4::Debug;
106
use C4::Log qw/ logaction /;
106
use C4::Log qw/ logaction /;
107
use Koha::Cache;
107
use POSIX ();
108
use POSIX ();
108
use DateTime::TimeZone;
109
use DateTime::TimeZone;
109
use Module::Load::Conditional qw(can_load);
110
use Module::Load::Conditional qw(can_load);
Lines 376-382 sub new { Link Here
376
            return;
377
            return;
377
        }
378
        }
378
    }
379
    }
379
    
380
380
    if ($ismemcached) {
381
    if ($ismemcached) {
381
        # retreive from memcached
382
        # retreive from memcached
382
        $self = $memcached->get('kohaconf');
383
        $self = $memcached->get('kohaconf');
Lines 519-585 sub ModZebrations { Link Here
519
	return _common_config($_[1],'serverinfo');
520
	return _common_config($_[1],'serverinfo');
520
}
521
}
521
522
522
=head2 _cache_preference($variable, [$value])
523
524
  _cache_preference('opacusercss', $css);
525
  my $val = _cache_preference('opacusercss');
526
527
An interface into the syspref caching. One argument to get a value, two to set
528
it. The single argument version will return C<undef> if the cache doesn't
529
contain the value. If the second argument is C<undef>, then the cache will be
530
cleared of that value.
531
532
The cache timeout length is 30 seconds when memcache is not in use. When
533
memcache is in use, it uses the default cache time for that.
534
535
=cut
536
537
my %syspref_cache;
538
my $use_syspref_cache = 1;
539
540
sub _cache_preference {
541
    return undef unless $use_syspref_cache;
542
    my $memcached        = memcached();
543
    my $memcached_prefix = "_syspref_";
544
    if ( scalar(@_) == 1 ) {
545
        # This means we want to fetch from the cache
546
        my ($var) = @_;
547
        if ($memcached) {
548
            my $val = $memcached->get( $memcached_prefix . lc($var) );
549
            return $val;
550
        }
551
        else {
552
            my $now          = time;
553
            my $cache_oldest = $now - 30;
554
            return undef unless exists $syspref_cache{ lc $var };
555
            my $val = $syspref_cache{ lc $var };
556
            return undef unless $val->[0] > $cache_oldest;
557
            return $val->[1];
558
        }
559
    }
560
    elsif ( scalar(@_) == 2 ) {
561
        # Set something in the cache
562
        my ( $var, $val ) = @_;
563
        if ($memcached) {
564
            if ( !defined($val) ) {
565
                $memcached->delete( $memcached_prefix . lc($var) );
566
            }
567
            else {
568
                $memcached->set( $memcached_prefix . lc($var), $val );
569
            }
570
        }
571
        else {
572
            if ( !defined($val) ) {
573
                delete $syspref_cache{ lc $var };
574
                return;
575
            }
576
            $syspref_cache{ lc $var } = [ time, $val ];
577
        }
578
    } else {
579
        confess "Invalid arguments to _cache_preference: " . join(', ', @_);
580
    }
581
}
582
583
=head2 preference
523
=head2 preference
584
524
585
  $sys_preference = C4::Context->preference('some_variable');
525
  $sys_preference = C4::Context->preference('some_variable');
Lines 596-606 with this method. Link Here
596
536
597
=cut
537
=cut
598
538
539
my $syspref_cache = Koha::Cache->get_instance();
540
my $use_syspref_cache = 1;
599
sub preference {
541
sub preference {
600
    my $self = shift;
542
    my $self = shift;
601
    my $var  = shift;    # The system preference to return
543
    my $var  = shift;    # The system preference to return
602
544
603
    my $cached_var = _cache_preference($var);
545
    my $cached_var = $syspref_cache->get_from_cache("syspref_$var") if $use_syspref_cache;
604
    return $cached_var if defined $cached_var;
546
    return $cached_var if defined $cached_var;
605
547
606
    my $dbh  = C4::Context->dbh or return 0;
548
    my $dbh  = C4::Context->dbh or return 0;
Lines 619-625 sub preference { Link Here
619
        $value = $dbh->selectrow_array( $sql, {}, lc $var );
561
        $value = $dbh->selectrow_array( $sql, {}, lc $var );
620
    }
562
    }
621
563
622
    _cache_preference($var, $value);
564
    $syspref_cache->set_in_cache("syspref_$var", $value) if $use_syspref_cache;
623
    return $value;
565
    return $value;
624
}
566
}
625
567
Lines 670-680 will not be seen by this process. Link Here
670
=cut
612
=cut
671
613
672
sub clear_syspref_cache {
614
sub clear_syspref_cache {
673
    %syspref_cache= ();
615
    $syspref_cache->flush_all if $use_syspref_cache;
674
    # It doesn't seem to be possible to list all the keys via Cache::Memcached
675
    # so we'll clear the whole cache.
676
    my $memcached = memcached();
677
    $memcached->flush_all if $memcached;
678
}
616
}
679
617
680
=head2 set_preference
618
=head2 set_preference
Lines 720-726 ON DUPLICATE KEY UPDATE value = VALUES(value)'; Link Here
720
    }
658
    }
721
    my $sth = $dbh->prepare($query);
659
    my $sth = $dbh->prepare($query);
722
    if ( $sth->execute(@params) ) {
660
    if ( $sth->execute(@params) ) {
723
        _cache_preference($var, $value);
661
        $syspref_cache->set_in_cache("syspref_$var", $value) if $use_syspref_cache;
724
        logaction( 'SYSTEMPREFERENCE', $logtype, undef, $var . " | " . $value );
662
        logaction( 'SYSTEMPREFERENCE', $logtype, undef, $var . " | " . $value );
725
    }
663
    }
726
    $sth->finish;
664
    $sth->finish;
Lines 745-751 sub delete_preference { Link Here
745
    my $sth = $dbh->prepare("DELETE FROM systempreferences WHERE variable=?");
683
    my $sth = $dbh->prepare("DELETE FROM systempreferences WHERE variable=?");
746
    my $res = $sth->execute($var);
684
    my $res = $sth->execute($var);
747
    if ($res) {
685
    if ($res) {
748
        _cache_preference($var, undef);
686
        $syspref_cache->clear_from_cache("syspref_$var") if $use_syspref_cache;
749
        logaction( 'SYSTEMPREFERENCE', 'DELETE', undef,
687
        logaction( 'SYSTEMPREFERENCE', 'DELETE', undef,
750
            $var . " | " . ( $val // 'undefined' ) );
688
            $var . " | " . ( $val // 'undefined' ) );
751
        return 1;
689
        return 1;
(-)a/t/db_dependent/Circulation.t (+1 lines)
Lines 55-60 my $borrower = { Link Here
55
};
55
};
56
56
57
# No userenv, PickupLibrary
57
# No userenv, PickupLibrary
58
C4::Context->disable_syspref_cache();
58
C4::Context->set_preference('CircControl', 'PickupLibrary');
59
C4::Context->set_preference('CircControl', 'PickupLibrary');
59
is(
60
is(
60
    C4::Context->preference('CircControl'),
61
    C4::Context->preference('CircControl'),
(-)a/t/db_dependent/Context.t (+1 lines)
Lines 53-58 $module->mock( Link Here
53
);
53
);
54
54
55
my $history;
55
my $history;
56
C4::Context->disable_syspref_cache();
56
$dbh = C4::Context->dbh({ new => 1 });
57
$dbh = C4::Context->dbh({ new => 1 });
57
58
58
$dbh->{mock_add_resultset} = [ ['value'], ['thing1'] ];
59
$dbh->{mock_add_resultset} = [ ['value'], ['thing1'] ];
(-)a/t/db_dependent/check_sysprefs.t (-1 / +1 lines)
Lines 23-28 use warnings; Link Here
23
use Getopt::Long;
23
use Getopt::Long;
24
use C4::Context;
24
use C4::Context;
25
25
26
C4::Context->disable_syspref_cache();
26
# When this option is set, no tests are performed.
27
# When this option is set, no tests are performed.
27
# The missing sysprefs are displayed as sql inserts instead.
28
# The missing sysprefs are displayed as sql inserts instead.
28
our $showsql = 0;
29
our $showsql = 0;
29
- 

Return to bug 11998