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

(-)a/C4/Context.pm (-17 / +66 lines)
Lines 519-524 sub ModZebrations { Link Here
519
	return _common_config($_[1],'serverinfo');
519
	return _common_config($_[1],'serverinfo');
520
}
520
}
521
521
522
=head2 _cache_preference($variable, [$value])
523
524
  _cache_preferences('opacusercss', $css);
525
  my $val = _cach_preferences('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
        my ($var) = @_;
546
        if ($memcached) {
547
            my $val = $memcached->get( $memcached_prefix . lc($var) );
548
            return $val;
549
        }
550
        else {
551
            my $now          = time;
552
            my $cache_oldest = $now - 30;
553
            return undef unless exists $syspref_cache{ lc $var };
554
            my $val = $syspref_cache{ lc $var };
555
            return undef unless $val->[0] > $cache_oldest;
556
            return $val->[1];
557
        }
558
    }
559
    elsif ( scalar(@_) == 2 ) {
560
        my ( $var, $val ) = @_;
561
        if ($memcached) {
562
            if ( !defined($val) ) {
563
                $memcached->delete( $memcached_prefix . lc($var) );
564
            }
565
            else {
566
                $memcached->set( $memcached_prefix . lc($var), $val );
567
            }
568
        }
569
        else {
570
            if ( !defined($val) ) {
571
                delete $syspref_cache{ lc $var };
572
                return;
573
            }
574
            $syspref_cache{ lc $var } = [ time, $val ];
575
        }
576
    }
577
}
578
522
=head2 preference
579
=head2 preference
523
580
524
  $sys_preference = C4::Context->preference('some_variable');
581
  $sys_preference = C4::Context->preference('some_variable');
Lines 535-557 with this method. Link Here
535
592
536
=cut
593
=cut
537
594
538
my %syspref_cache;
539
my $use_syspref_cache = 1;
540
541
sub preference {
595
sub preference {
542
    my $self = shift;
596
    my $self = shift;
543
    my $var  = shift;    # The system preference to return
597
    my $var  = shift;    # The system preference to return
544
598
545
    # We cache for 30 seconds, after that sysprefs are expired from the cache.
599
    my $cached_var = _cache_preference($var);
546
    # This is for persistent environments like Plack.
600
    return $cached_var if defined $cached_var;
547
    my $now = time;
548
    my $cache_oldest = $now - 30;
549
    if ($use_syspref_cache && exists $syspref_cache{lc $var}) {
550
        # The cache contains an arrayref, first entry is the time it was
551
        # stored, the second is the value.
552
        my $cached_value = $syspref_cache{lc $var};
553
        return $cached_value->[1] if ($cached_value->[0] > $cache_oldest);
554
    }
555
601
556
    my $dbh  = C4::Context->dbh or return 0;
602
    my $dbh  = C4::Context->dbh or return 0;
557
603
Lines 569-575 sub preference { Link Here
569
        $value = $dbh->selectrow_array( $sql, {}, lc $var );
615
        $value = $dbh->selectrow_array( $sql, {}, lc $var );
570
    }
616
    }
571
617
572
    $syspref_cache{lc $var} = [$now, $value];
618
    _cache_preference($var, $value);
573
    return $value;
619
    return $value;
574
}
620
}
575
621
Lines 621-626 will not be seen by this process. Link Here
621
667
622
sub clear_syspref_cache {
668
sub clear_syspref_cache {
623
    %syspref_cache= ();
669
    %syspref_cache= ();
670
    # It doesn't seem to be possible to list all the keys via Cache::Memcached
671
    # so we'll clear the whole cache.
672
    my $memcached = memcached();
673
    $memcached->flush_all if $memcached;
624
}
674
}
625
675
626
=head2 set_preference
676
=head2 set_preference
Lines 666-672 ON DUPLICATE KEY UPDATE value = VALUES(value)'; Link Here
666
    }
716
    }
667
    my $sth = $dbh->prepare($query);
717
    my $sth = $dbh->prepare($query);
668
    if ( $sth->execute(@params) ) {
718
    if ( $sth->execute(@params) ) {
669
        $syspref_cache{$var} = [ time, $value ];
719
        _cache_preference($var, $value);
670
        logaction( 'SYSTEMPREFERENCE', $logtype, undef, $var . " | " . $value );
720
        logaction( 'SYSTEMPREFERENCE', $logtype, undef, $var . " | " . $value );
671
    }
721
    }
672
    $sth->finish;
722
    $sth->finish;
Lines 691-697 sub delete_preference { Link Here
691
    my $sth = $dbh->prepare("DELETE FROM systempreferences WHERE variable=?");
741
    my $sth = $dbh->prepare("DELETE FROM systempreferences WHERE variable=?");
692
    my $res = $sth->execute($var);
742
    my $res = $sth->execute($var);
693
    if ($res) {
743
    if ($res) {
694
        delete $syspref_cache{$var};
744
        C4::Context->set_preference($var, undef);
695
        logaction( 'SYSTEMPREFERENCE', 'DELETE', undef,
745
        logaction( 'SYSTEMPREFERENCE', 'DELETE', undef,
696
            $var . " | " . ( $var // 'undefined' ) );
746
            $var . " | " . ( $var // 'undefined' ) );
697
        return 1;
747
        return 1;
698
- 

Return to bug 11998