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

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

Return to bug 11998