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

(-)a/C4/Auth.pm (-5 / +5 lines)
Lines 31-37 use C4::Templates; # to get the template Link Here
31
use C4::Languages;
31
use C4::Languages;
32
use C4::Search::History;
32
use C4::Search::History;
33
use Koha;
33
use Koha;
34
use Koha::Caches;
35
use Koha::AuthUtils qw(get_script_name hash_password);
34
use Koha::AuthUtils qw(get_script_name hash_password);
36
use Koha::Libraries;
35
use Koha::Libraries;
37
use Koha::LibraryCategories;
36
use Koha::LibraryCategories;
Lines 1733-1743 sub get_session { Link Here
1733
    elsif ( $storage_method eq 'Pg' ) {
1732
    elsif ( $storage_method eq 'Pg' ) {
1734
        $session = new CGI::Session( "driver:PostgreSQL;serializer:yaml;id:md5", $sessionID, { Handle => $dbh } );
1733
        $session = new CGI::Session( "driver:PostgreSQL;serializer:yaml;id:md5", $sessionID, { Handle => $dbh } );
1735
    }
1734
    }
1736
    elsif ( $storage_method eq 'memcached' && Koha::Caches->get_instance->memcached_cache ) {
1735
    elsif ( $storage_method eq 'memcached' ) {
1737
        my $memcached = Koha::Caches->get_instance()->memcached_cache;
1736
        if ( my $memcached = C4::Context->cache->memcached_cache ) {
1738
        $session = new CGI::Session( "driver:memcached;serializer:yaml;id:md5", $sessionID, { Memcached => $memcached } );
1737
            $session = new CGI::Session( "driver:memcached;serializer:yaml;id:md5", $sessionID, { Memcached => $memcached } );
1738
        }
1739
    }
1739
    }
1740
    else {
1740
    unless ($session) {
1741
        # catch all defaults to tmp should work on all systems
1741
        # catch all defaults to tmp should work on all systems
1742
        my $dir = File::Spec->tmpdir;
1742
        my $dir = File::Spec->tmpdir;
1743
        my $instance = C4::Context->config( 'database' ); #actually for packages not exactly the instance name, but generally safer to leave it as it is
1743
        my $instance = C4::Context->config( 'database' ); #actually for packages not exactly the instance name, but generally safer to leave it as it is
(-)a/C4/Biblio.pm (-3 / +3 lines)
Lines 37-44 use C4::Charset; Link Here
37
use C4::Linker;
37
use C4::Linker;
38
use C4::OAI::Sets;
38
use C4::OAI::Sets;
39
use C4::Debug;
39
use C4::Debug;
40
use C4::Context;
40
41
41
use Koha::Caches;
42
use Koha::Authority::Types;
42
use Koha::Authority::Types;
43
use Koha::Acquisition::Currencies;
43
use Koha::Acquisition::Currencies;
44
use Koha::SearchEngine;
44
use Koha::SearchEngine;
Lines 1136-1142 sub GetMarcStructure { Link Here
1136
1136
1137
    $forlibrarian = $forlibrarian ? 1 : 0;
1137
    $forlibrarian = $forlibrarian ? 1 : 0;
1138
    my $unsafe = ($params && $params->{unsafe})? 1: 0;
1138
    my $unsafe = ($params && $params->{unsafe})? 1: 0;
1139
    my $cache = Koha::Caches->get_instance();
1139
    my $cache = C4::Context->cache;
1140
    my $cache_key = "MarcStructure-$forlibrarian-$frameworkcode";
1140
    my $cache_key = "MarcStructure-$forlibrarian-$frameworkcode";
1141
    my $cached = $cache->get_from_cache($cache_key, { unsafe => $unsafe });
1141
    my $cached = $cache->get_from_cache($cache_key, { unsafe => $unsafe });
1142
    return $cached if $cached;
1142
    return $cached if $cached;
Lines 1244-1250 sub GetMarcSubfieldStructure { Link Here
1244
1244
1245
    $frameworkcode //= '';
1245
    $frameworkcode //= '';
1246
1246
1247
    my $cache     = Koha::Caches->get_instance();
1247
    my $cache     = C4::Context->cache;
1248
    my $cache_key = "MarcSubfieldStructure-$frameworkcode";
1248
    my $cache_key = "MarcSubfieldStructure-$frameworkcode";
1249
    my $cached    = $cache->get_from_cache($cache_key);
1249
    my $cached    = $cache->get_from_cache($cache_key);
1250
    return $cached if $cached;
1250
    return $cached if $cached;
(-)a/C4/Calendar.pm (-8 / +7 lines)
Lines 23-29 use Carp; Link Here
23
use Date::Calc qw( Date_to_Days Today);
23
use Date::Calc qw( Date_to_Days Today);
24
24
25
use C4::Context;
25
use C4::Context;
26
use Koha::Caches;
27
26
28
use constant ISO_DATE_FORMAT => "%04d-%02d-%02d";
27
use constant ISO_DATE_FORMAT => "%04d-%02d-%02d";
29
28
Lines 276-282 sub insert_single_holiday { Link Here
276
275
277
276
278
    # changed the 'single_holidays' table, lets force/reset its cache
277
    # changed the 'single_holidays' table, lets force/reset its cache
279
    my $cache = Koha::Caches->get_instance();
278
    my $cache = C4::Context->cache;
280
    $cache->clear_from_cache( 'single_holidays') ;
279
    $cache->clear_from_cache( 'single_holidays') ;
281
    $cache->clear_from_cache( 'exception_holidays') ;
280
    $cache->clear_from_cache( 'exception_holidays') ;
282
281
Lines 321-327 sub insert_exception_holiday { Link Here
321
    $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
320
    $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
322
321
323
    # changed the 'single_holidays' table, lets force/reset its cache
322
    # changed the 'single_holidays' table, lets force/reset its cache
324
    my $cache = Koha::Caches->get_instance();
323
    my $cache = C4::Context->cache;
325
    $cache->clear_from_cache( 'single_holidays') ;
324
    $cache->clear_from_cache( 'single_holidays') ;
326
    $cache->clear_from_cache( 'exception_holidays') ;
325
    $cache->clear_from_cache( 'exception_holidays') ;
327
326
Lines 422-428 UPDATE special_holidays SET title = ?, description = ? Link Here
422
    $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
421
    $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
423
422
424
    # changed the 'single_holidays' table, lets force/reset its cache
423
    # changed the 'single_holidays' table, lets force/reset its cache
425
    my $cache = Koha::Caches->get_instance();
424
    my $cache = C4::Context->cache;
426
    $cache->clear_from_cache( 'single_holidays') ;
425
    $cache->clear_from_cache( 'single_holidays') ;
427
    $cache->clear_from_cache( 'exception_holidays') ;
426
    $cache->clear_from_cache( 'exception_holidays') ;
428
427
Lines 465-471 UPDATE special_holidays SET title = ?, description = ? Link Here
465
    $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
464
    $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
466
465
467
    # changed the 'single_holidays' table, lets force/reset its cache
466
    # changed the 'single_holidays' table, lets force/reset its cache
468
    my $cache = Koha::Caches->get_instance();
467
    my $cache = C4::Context->cache;
469
    $cache->clear_from_cache( 'single_holidays') ;
468
    $cache->clear_from_cache( 'single_holidays') ;
470
    $cache->clear_from_cache( 'exception_holidays') ;
469
    $cache->clear_from_cache( 'exception_holidays') ;
471
470
Lines 546-552 sub delete_holiday { Link Here
546
    }
545
    }
547
546
548
    # changed the 'single_holidays' table, lets force/reset its cache
547
    # changed the 'single_holidays' table, lets force/reset its cache
549
    my $cache = Koha::Caches->get_instance();
548
    my $cache = C4::Context->cache;
550
    $cache->clear_from_cache( 'single_holidays') ;
549
    $cache->clear_from_cache( 'single_holidays') ;
551
    $cache->clear_from_cache( 'exception_holidays') ;
550
    $cache->clear_from_cache( 'exception_holidays') ;
552
551
Lines 577-583 sub delete_holiday_range { Link Here
577
    $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
576
    $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
578
577
579
    # changed the 'single_holidays' table, lets force/reset its cache
578
    # changed the 'single_holidays' table, lets force/reset its cache
580
    my $cache = Koha::Caches->get_instance();
579
    my $cache = C4::Context->cache;
581
    $cache->clear_from_cache( 'single_holidays') ;
580
    $cache->clear_from_cache( 'single_holidays') ;
582
    $cache->clear_from_cache( 'exception_holidays') ;
581
    $cache->clear_from_cache( 'exception_holidays') ;
583
582
Lines 631-637 sub delete_exception_holiday_range { Link Here
631
    $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
630
    $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
632
631
633
    # changed the 'single_holidays' table, lets force/reset its cache
632
    # changed the 'single_holidays' table, lets force/reset its cache
634
    my $cache = Koha::Caches->get_instance();
633
    my $cache = C4::Context->cache;
635
    $cache->clear_from_cache( 'single_holidays') ;
634
    $cache->clear_from_cache( 'single_holidays') ;
636
    $cache->clear_from_cache( 'exception_holidays') ;
635
    $cache->clear_from_cache( 'exception_holidays') ;
637
}
636
}
(-)a/C4/Context.pm (-14 / +40 lines)
Lines 178-183 sub current { Link Here
178
    return $context;
178
    return $context;
179
}
179
}
180
180
181
=head2 cache
182
183
Returns the cache object or undef
184
185
=cut
186
187
sub cache {
188
    my $self = shift;
189
    $self = $context unless ref ($self);
190
191
    return Koha::Caches->get_instance($self->{namespace});
192
}
193
194
=head2 memcached
195
196
Returns the cache object if it is memcached or undef
197
198
=cut
199
200
sub memcached {
201
    my $self = shift;
202
    my $cache = $self->cache;
203
    return $cache if $cache->{memcached_cache};
204
205
    return;
206
}
207
181
sub db_driver {
208
sub db_driver {
182
    my $self = shift;
209
    my $self = shift;
183
210
Lines 220-230 sub import { Link Here
220
    # multihost, we don't want/have one default config
247
    # multihost, we don't want/have one default config
221
    return if $config_file && $config_file eq ":no_config";
248
    return if $config_file && $config_file eq ":no_config";
222
249
223
    my $conf_cache = Koha::Caches->get_instance('config');
250
    my $cache = Koha::Caches->get_instance();
224
    if ( $conf_cache->cache ) {
251
    undef $cache unless $cache->{memcached_cache};
225
        if ( $context = $conf_cache->get_from_cache('koha_conf') ) {
252
    if ( $cache and $context = $cache->get_from_cache('koha_conf') ) {
226
            return;
253
        return;
227
        }
228
    }
254
    }
229
255
230
    undef $config_file if $config_file && !-s $config_file;
256
    undef $config_file if $config_file && !-s $config_file;
Lines 232-241 sub import { Link Here
232
    $context = __PACKAGE__->new($config_file);
258
    $context = __PACKAGE__->new($config_file);
233
    return unless $context;
259
    return unless $context;
234
260
235
    if ( $conf_cache->memcached_cache ) {
261
    if ( $cache ) {
236
        # FIXME it may be better to use the memcached servers from the config file
262
        # FIXME it may be better to use the memcached servers from the config file
237
        # to cache it
263
        # to cache it
238
        $conf_cache->set_in_cache('koha_conf', $context)
264
        $cache->set_in_cache('koha_conf', $context)
239
    }
265
    }
240
}
266
}
241
267
Lines 266-272 sub new { Link Here
266
    $self->{config_file} = $conf_fname;
292
    $self->{config_file} = $conf_fname;
267
    $self->{namespace} = $namespace;
293
    $self->{namespace} = $namespace;
268
    $self->{use_syspref_cache} = 1;
294
    $self->{use_syspref_cache} = 1;
269
    $self->{syspref_cache} = Koha::Cache->new({namespace => $namespace});
270
295
271
    $self->{"Zconn"} = undef;    # Zebra Connections
296
    $self->{"Zconn"} = undef;    # Zebra Connections
272
    $self->{"userenv"} = undef;        # User env
297
    $self->{"userenv"} = undef;        # User env
Lines 446-452 sub preference { Link Here
446
        if defined $ENV{"OVERRIDE_SYSPREF_$var"};
471
        if defined $ENV{"OVERRIDE_SYSPREF_$var"};
447
472
448
    my $cached_var = $self->{use_syspref_cache}
473
    my $cached_var = $self->{use_syspref_cache}
449
        ? $self->{syspref_cache}->get_from_cache("syspref_$var")
474
        ? $self->cache->get_from_cache("syspref_$var")
450
        : undef;
475
        : undef;
451
    return $cached_var if defined $cached_var;
476
    return $cached_var if defined $cached_var;
452
477
Lines 455-461 sub preference { Link Here
455
    my $value = $syspref ? $syspref->value() : undef;
480
    my $value = $syspref ? $syspref->value() : undef;
456
481
457
    if ( $self->{use_syspref_cache} ) {
482
    if ( $self->{use_syspref_cache} ) {
458
        $self->{syspref_cache}->set_in_cache("syspref_$var", $value);
483
        $self->cache->set_in_cache("syspref_$var", $value);
484
        $self->{sysprefs}{$var} = $value if $self;
459
    }
485
    }
460
    return $value;
486
    return $value;
461
}
487
}
Lines 496-503 used with Plack and other persistent environments. Link Here
496
sub disable_syspref_cache {
522
sub disable_syspref_cache {
497
    my ($self) = @_;
523
    my ($self) = @_;
498
    $self = $context unless ref $self;
524
    $self = $context unless ref $self;
499
    $self->{use_syspref_cache} = 0;
500
    $self->clear_syspref_cache();
525
    $self->clear_syspref_cache();
526
    $self->{use_syspref_cache} = 0;
501
}
527
}
502
528
503
=head2 clear_syspref_cache
529
=head2 clear_syspref_cache
Lines 514-520 sub clear_syspref_cache { Link Here
514
    my ($self) = @_;
540
    my ($self) = @_;
515
    $self = $context unless ref $self;
541
    $self = $context unless ref $self;
516
    return unless $self->{use_syspref_cache};
542
    return unless $self->{use_syspref_cache};
517
    $self->{syspref_cache}->flush_all;
543
    $self->cache->flush_all;
518
}
544
}
519
545
520
=head2 set_preference
546
=head2 set_preference
Lines 567-573 sub set_preference { Link Here
567
    }
593
    }
568
594
569
    if ( $self->{use_syspref_cache} ) {
595
    if ( $self->{use_syspref_cache} ) {
570
        $self->{syspref_cache}->set_in_cache( "syspref_$variable", $value );
596
        $self->cache->set_in_cache( "syspref_$variable", $value );
571
    }
597
    }
572
598
573
    return $syspref;
599
    return $syspref;
Lines 589-595 sub delete_preference { Link Here
589
615
590
    if ( Koha::Config::SysPrefs->find( $var )->delete ) {
616
    if ( Koha::Config::SysPrefs->find( $var )->delete ) {
591
        if ( $self->{use_syspref_cache} ) {
617
        if ( $self->{use_syspref_cache} ) {
592
            $self->{syspref_cache}->clear_from_cache("syspref_$var");
618
            $self->cache->clear_from_cache("syspref_$var");
593
        }
619
        }
594
620
595
        return 1;
621
        return 1;
(-)a/C4/External/OverDrive.pm (-4 / +2 lines)
Lines 22-28 use warnings; Link Here
22
22
23
use Koha;
23
use Koha;
24
use JSON;
24
use JSON;
25
use Koha::Caches;
25
use C4::Context;
26
use HTTP::Request;
26
use HTTP::Request;
27
use HTTP::Request::Common;
27
use HTTP::Request::Common;
28
use LWP::Authen::Basic;
28
use LWP::Authen::Basic;
Lines 97-105 sub GetOverDriveToken { Link Here
97
97
98
    return unless ( $key && $secret ) ;
98
    return unless ( $key && $secret ) ;
99
99
100
    my $cache;
100
    my $cache = C4::Context->cache;
101
102
    eval { $cache = Koha::Caches->get_instance() };
103
101
104
    my $token;
102
    my $token;
105
    $cache and $token = $cache->get_from_cache( "overdrive_token" ) and return $token;
103
    $cache and $token = $cache->get_from_cache( "overdrive_token" ) and return $token;
(-)a/C4/Items.pm (-1 / +1 lines)
Lines 459-465 Returns item record Link Here
459
sub _build_default_values_for_mod_marc {
459
sub _build_default_values_for_mod_marc {
460
    my ($frameworkcode) = @_;
460
    my ($frameworkcode) = @_;
461
461
462
    my $cache     = Koha::Caches->get_instance();
462
    my $cache     = C4::Context->cache;
463
    my $cache_key = "default_value_for_mod_marc-$frameworkcode";
463
    my $cache_key = "default_value_for_mod_marc-$frameworkcode";
464
    my $cached    = $cache->get_from_cache($cache_key);
464
    my $cached    = $cache->get_from_cache($cache_key);
465
    return $cached if $cached;
465
    return $cached if $cached;
(-)a/C4/Koha.pm (-2 / +1 lines)
Lines 24-30 use strict; Link Here
24
#use warnings; FIXME - Bug 2505
24
#use warnings; FIXME - Bug 2505
25
25
26
use C4::Context;
26
use C4::Context;
27
use Koha::Caches;
28
use Koha::DateUtils qw(dt_from_string);
27
use Koha::DateUtils qw(dt_from_string);
29
use Koha::Libraries;
28
use Koha::Libraries;
30
use DateTime::Format::MySQL;
29
use DateTime::Format::MySQL;
Lines 942-948 sub GetAuthorisedValues { Link Here
942
      C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
941
      C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
943
    my $cache_key =
942
    my $cache_key =
944
      "AuthorisedValues-$category-$opac-$branch_limit";
943
      "AuthorisedValues-$category-$opac-$branch_limit";
945
    my $cache  = Koha::Caches->get_instance();
944
    my $cache  = C4::Context->cache;
946
    my $result = $cache->get_from_cache($cache_key);
945
    my $result = $cache->get_from_cache($cache_key);
947
    return $result if $result;
946
    return $result if $result;
948
947
(-)a/C4/Utils/DataTables/ColumnsSettings.pm (-2 / +1 lines)
Lines 5-15 use List::Util qw( first ); Link Here
5
use YAML;
5
use YAML;
6
use C4::Context;
6
use C4::Context;
7
use Koha::Database;
7
use Koha::Database;
8
use Koha::Caches;
9
8
10
sub get_yaml {
9
sub get_yaml {
11
    my $yml_path = C4::Context->config('intranetdir') . '/admin/columns_settings.yml';
10
    my $yml_path = C4::Context->config('intranetdir') . '/admin/columns_settings.yml';
12
    my $cache = Koha::Caches->get_instance();
11
    my $cache = C4::Context->cache;
13
    my $yaml  = $cache->get_from_cache('ColumnsSettingsYaml');
12
    my $yaml  = $cache->get_from_cache('ColumnsSettingsYaml');
14
13
15
    unless ($yaml) {
14
    unless ($yaml) {
(-)a/Koha/Cache.pm (-26 / +17 lines)
Lines 54-69 our %L1_cache; Link Here
54
our $L1_encoder = Sereal::Encoder->new;
54
our $L1_encoder = Sereal::Encoder->new;
55
our $L1_decoder = Sereal::Decoder->new;
55
our $L1_decoder = Sereal::Decoder->new;
56
56
57
=head2 get_instance
58
59
    my $cache = Koha::Caches->get_instance();
60
61
This gets a shared instance of the cache, set up in a very default way. This is
62
the recommended way to fetch a cache object. If possible, it'll be
63
persistent across multiple instances.
64
65
=cut
66
67
=head2 new
57
=head2 new
68
58
69
Create a new Koha::Cache object. This is required for all cache-related functionality.
59
Create a new Koha::Cache object. This is required for all cache-related functionality.
Lines 71-104 Create a new Koha::Cache object. This is required for all cache-related function Link Here
71
=cut
61
=cut
72
62
73
sub new {
63
sub new {
74
    my ( $class, $self, $params ) = @_;
64
    my ( $class, $self ) = @_;
75
    $self->{'default_type'} =
65
    $self->{'default_type'} =
76
         $self->{cache_type}
66
         $self->{cache_type}
77
      || $ENV{CACHING_SYSTEM} # DELME What about this?
67
      || $ENV{CACHING_SYSTEM} # DELME What about this?
78
      || 'memcached';
68
      || 'memcached';
79
69
80
    my $subnamespace = $params->{subnamespace} // '';
70
    $self->{namespace} //= '';
81
71
82
    $ENV{DEBUG} && carp "Default caching system: $self->{'default_type'}";
72
    $ENV{DEBUG} && carp "Default caching system: $self->{'default_type'}";
83
73
84
    $self->{'timeout'}   ||= 0;
74
    $self->{'timeout'}   ||= 0;
85
    # Should we continue to support MEMCACHED ENV vars?
86
    $self->{'namespace'} ||= $ENV{MEMCACHED_NAMESPACE};
87
    my @servers = split /,/, $ENV{MEMCACHED_SERVERS} || '';
88
    unless ( $self->{namespace} and @servers ) {
89
        my $koha_config = Koha::Config->read_from_file( Koha::Config->guess_koha_conf() );
90
        $self->{namespace} ||= $koha_config->{config}{memcached_namespace} || 'koha';
91
        @servers = split /,/, $koha_config->{config}{memcached_servers} // ''
92
            unless @servers;
93
    }
94
    $self->{namespace} .= ":$subnamespace:";
95
75
96
    if ( $self->{'default_type'} eq 'memcached'
76
    if ( $self->{'default_type'} eq 'memcached'
97
        && can_load( modules => { 'Cache::Memcached::Fast' => undef } )
77
        && can_load( modules => { 'Cache::Memcached::Fast' => undef } ) )
98
        && _initialize_memcached($self, @servers)
99
        && defined( $self->{'memcached_cache'} ) )
100
    {
78
    {
101
        $self->{'cache'} = $self->{'memcached_cache'};
79
        # Should we continue to support MEMCACHED ENV vars?
80
        $self->{'namespace'} ||= $ENV{MEMCACHED_NAMESPACE};
81
        my @servers = split /,/, $ENV{MEMCACHED_SERVERS} || '';
82
        unless ( $self->{namespace} && @servers ) {
83
            my $koha_config = Koha::Config->read_from_file( Koha::Config->guess_koha_conf() );
84
            $self->{namespace} ||= $koha_config->{config}{memcached_namespace} || 'koha';
85
            @servers = split /,/, $koha_config->{config}{memcached_servers} // ''
86
                unless @servers;
87
        }
88
        if ( _initialize_memcached($self, @servers)
89
            && defined( $self->{'memcached_cache'} ) )
90
        {
91
            $self->{'cache'} = $self->{'memcached_cache'};
92
        }
102
    }
93
    }
103
94
104
    if ( $self->{'default_type'} eq 'fastmmap'
95
    if ( $self->{'default_type'} eq 'fastmmap'
(-)a/Koha/Caches.pm (-5 / +5 lines)
Lines 21-31 use Koha::Cache; Link Here
21
21
22
our $singleton_caches;
22
our $singleton_caches;
23
sub get_instance {
23
sub get_instance {
24
    my ($class, $subnamespace) = @_;
24
    my ($class, $namespace) = @_;
25
    $subnamespace //= '';
25
    $namespace //= '';
26
    $singleton_caches->{$subnamespace} = Koha::Cache->new({}, { subnamespace => $subnamespace } )
26
    $singleton_caches->{$namespace} = Koha::Cache->new({ namespace => $namespace } )
27
        unless $singleton_caches->{$subnamespace};
27
        unless $singleton_caches->{$namespace};
28
    return $singleton_caches->{$subnamespace};
28
    return $singleton_caches->{$namespace};
29
}
29
}
30
30
31
sub flush_L1_caches {
31
sub flush_L1_caches {
(-)a/Koha/Calendar.pm (-3 / +2 lines)
Lines 7-13 use DateTime; Link Here
7
use DateTime::Set;
7
use DateTime::Set;
8
use DateTime::Duration;
8
use DateTime::Duration;
9
use C4::Context;
9
use C4::Context;
10
use Koha::Caches;
11
use Carp;
10
use Carp;
12
11
13
sub new {
12
sub new {
Lines 55-61 sub _init { Link Here
55
sub exception_holidays {
54
sub exception_holidays {
56
    my ( $self ) = @_;
55
    my ( $self ) = @_;
57
56
58
    my $cache  = Koha::Caches->get_instance();
57
    my $cache  = C4::Context->cache;
59
    my $cached = $cache->get_from_cache('exception_holidays');
58
    my $cached = $cache->get_from_cache('exception_holidays');
60
    return $cached if $cached;
59
    return $cached if $cached;
61
60
Lines 84-90 sub exception_holidays { Link Here
84
sub single_holidays {
83
sub single_holidays {
85
    my ( $self, $date ) = @_;
84
    my ( $self, $date ) = @_;
86
    my $branchcode = $self->{branchcode};
85
    my $branchcode = $self->{branchcode};
87
    my $cache           = Koha::Caches->get_instance();
86
    my $cache           = C4::Context->cache;
88
    my $single_holidays = $cache->get_from_cache('single_holidays');
87
    my $single_holidays = $cache->get_from_cache('single_holidays');
89
88
90
    # $single_holidays looks like:
89
    # $single_holidays looks like:
(-)a/Koha/Handler/Plack.pm (-2 / +39 lines)
Lines 69-74 use Plack::App::URLMap; Link Here
69
          hostname => 'koha1.com',
69
          hostname => 'koha1.com',
70
          app => $app1,
70
          app => $app1,
71
          context => $context1,
71
          context => $context1,
72
          shared_context => 1
72
      },
73
      },
73
      {
74
      {
74
          hostname => ['koha2.com', 'www.koha2.com'],
75
          hostname => ['koha2.com', 'www.koha2.com'],
Lines 78-90 use Plack::App::URLMap; Link Here
78
      ...
79
      ...
79
80
80
  C<hostname> is mandatory.
81
  C<hostname> is mandatory.
82
  If C<shared_context> is set to true, some Context properties will be preserved across
83
  forked processes. Useful if both OPAC and Intranet apps are served here, so no restart
84
  is needed when Context cached properties cnamge values. Needs memcached.
81
85
82
  koha.psgi:
86
  koha.psgi:
83
87
84
  use Plack::Builder;
88
  use Plack::Builder;
85
  use Plack::App::CGIBin;
89
  use Plack::App::CGIBin;
86
90
87
  use C4::Context;
91
  use C4::Context ":no_config";
88
92
89
  my $opac_app = builder {
93
  my $opac_app = builder {
90
      enable "Plack::Middleware::Static",
94
      enable "Plack::Middleware::Static",
Lines 137-142 use Plack::App::URLMap; Link Here
137
141
138
=cut
142
=cut
139
143
144
# We cannot store whole Context object, may contain non-serializable things
145
my @CONTEXT_UNSAFE_PROPERTIES = qw(sysprefs);
140
sub app_per_host {
146
sub app_per_host {
141
    my $class = shift;
147
    my $class = shift;
142
    my $sites = shift or die "No sites spec";
148
    my $sites = shift or die "No sites spec";
Lines 148-159 sub app_per_host { Link Here
148
154
149
        my $app = $site_params->{app} or croak "No app";
155
        my $app = $site_params->{app} or croak "No app";
150
        my $context = $site_params->{context} or croak "No Koha Context";
156
        my $context = $site_params->{context} or croak "No Koha Context";
157
        my $shared_context = $site_params->{shared_context};
158
        my $cache = $context->memcached;
159
        if ($shared_context) {
160
            if ($cache) {
161
                foreach (@CONTEXT_UNSAFE_PROPERTIES) {
162
                    # Clean slate
163
                    $cache->clear_from_cache($_);
164
                }
165
            }
166
            else {
167
                warn "shared_context works only with memcached";
168
            }
169
        }
151
170
152
        foreach my $host (@$hosts) {
171
        foreach my $host (@$hosts) {
153
            $map->map("http://$host/" => sub {
172
            $map->map("http://$host/" => sub {
154
                my $env = shift;
173
                my $env = shift;
155
174
156
                return $context->run_within_context(sub { $app->($env) });
175
                # may have stopped meanwhile or whatever
176
                my $cache = $context->memcached;
177
                if ($shared_context && $cache) {
178
                    foreach (@CONTEXT_UNSAFE_PROPERTIES) {
179
                        if (my $shared = $cache->get($_)) {
180
                            $context->{$_} = $shared;
181
                        }
182
                    }
183
                }
184
185
                my $ret = $context->run_within_context(sub { $app->($env) });
186
187
                if ($shared_context && $cache) {
188
                    foreach (@CONTEXT_UNSAFE_PROPERTIES) {
189
                        $cache->set($_, $context->{$_});
190
                    }
191
                }
192
193
                $ret;
157
            });
194
            });
158
        }
195
        }
159
    }
196
    }
(-)a/Koha/Handler/Plack/CGI.pm (-1 / +1 lines)
Lines 73-79 use Plack::App::CGIBin; Link Here
73
73
74
use parent "Koha::Handler::Plack";
74
use parent "Koha::Handler::Plack";
75
75
76
use C4::Context;
76
use C4::Context ":no_config";
77
77
78
=head1 CLASS METHODS
78
=head1 CLASS METHODS
79
79
(-)a/Koha/MetaSearcher.pm (-2 / +2 lines)
Lines 25-31 use C4::Charset qw( MarcToUTF8Record ); Link Here
25
use C4::Search qw(); # Purely for new_record_from_zebra
25
use C4::Search qw(); # Purely for new_record_from_zebra
26
use DBIx::Class::ResultClass::HashRefInflator;
26
use DBIx::Class::ResultClass::HashRefInflator;
27
use IO::Select;
27
use IO::Select;
28
use Koha::Caches;
28
use C4::Context;
29
use Koha::Database;
29
use Koha::Database;
30
use Koha::MetadataRecord;
30
use Koha::MetadataRecord;
31
use MARC::File::XML;
31
use MARC::File::XML;
Lines 91-97 sub search { Link Here
91
91
92
    my $resultset_expiry = 300;
92
    my $resultset_expiry = 300;
93
93
94
    my $cache = Koha::Caches->get_instance();
94
    my $cache = C4::Context->cache;
95
    my $schema = Koha::Database->new->schema;
95
    my $schema = Koha::Database->new->schema;
96
    my $stats = {
96
    my $stats = {
97
        num_fetched => {
97
        num_fetched => {
(-)a/Koha/Template/Plugin/Cache.pm (-3 / +1 lines)
Lines 21-27 use strict; Link Here
21
use warnings;
21
use warnings;
22
use base qw( Template::Plugin );
22
use base qw( Template::Plugin );
23
use Template::Plugin;
23
use Template::Plugin;
24
use C4::Context;
25
24
26
#------------------------------------------------------------------------
25
#------------------------------------------------------------------------
27
# new(\%options)
26
# new(\%options)
Lines 34-41 sub new { Link Here
34
        $cache = delete $params->{cache};
33
        $cache = delete $params->{cache};
35
    }
34
    }
36
    else {
35
    else {
37
        require Koha::Cache;
36
        $cache = $context->cache;
38
        $cache = Koha::Caches->get_instance();
39
    }
37
    }
40
    my $self = bless {
38
    my $self = bless {
41
        CACHE   => $cache,
39
        CACHE   => $cache,
(-)a/admin/biblio_framework.pl (-2 / +1 lines)
Lines 26-37 use C4::Output; Link Here
26
use Koha::Biblios;
26
use Koha::Biblios;
27
use Koha::BiblioFramework;
27
use Koha::BiblioFramework;
28
use Koha::BiblioFrameworks;
28
use Koha::BiblioFrameworks;
29
use Koha::Caches;
30
29
31
my $input         = new CGI;
30
my $input         = new CGI;
32
my $frameworkcode = $input->param('frameworkcode') || q||;
31
my $frameworkcode = $input->param('frameworkcode') || q||;
33
my $op            = $input->param('op') || q|list|;
32
my $op            = $input->param('op') || q|list|;
34
my $cache         = Koha::Caches->get_instance();
33
my $cache         = C4::Context->cache;
35
my @messages;
34
my @messages;
36
35
37
my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36
my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
(-)a/admin/koha2marclinks.pl (-1 / +1 lines)
Lines 59-65 else { Link Here
59
}
59
}
60
60
61
my $dbh = C4::Context->dbh;
61
my $dbh = C4::Context->dbh;
62
my $cache = Koha::Caches->get_instance();
62
my $cache = C4::Context->cache;
63
63
64
################## ADD_FORM ##################################
64
################## ADD_FORM ##################################
65
# called by default. Used to create form to add or  modify a record
65
# called by default. Used to create form to add or  modify a record
(-)a/admin/marc_subfields_structure.pl (-1 / +1 lines)
Lines 77-83 my ( $template, $borrowernumber, $cookie ) = get_template_and_user( Link Here
77
        debug           => 1,
77
        debug           => 1,
78
    }
78
    }
79
);
79
);
80
my $cache = Koha::Caches->get_instance();
80
my $cache = C4::Context->cache;
81
81
82
my $op       = $input->param('op') || "";
82
my $op       = $input->param('op') || "";
83
$tagfield =~ s/\,//g;
83
$tagfield =~ s/\,//g;
(-)a/admin/marctagstructure.pl (-4 / +1 lines)
Lines 25-33 use C4::Auth; Link Here
25
use C4::Koha;
25
use C4::Koha;
26
use C4::Context;
26
use C4::Context;
27
use C4::Output;
27
use C4::Output;
28
use C4::Context;
29
30
use Koha::Caches;
31
28
32
# retrieve parameters
29
# retrieve parameters
33
my $input = new CGI;
30
my $input = new CGI;
Lines 46-52 my $pagesize = 20; Link Here
46
my $script_name = "/cgi-bin/koha/admin/marctagstructure.pl";
43
my $script_name = "/cgi-bin/koha/admin/marctagstructure.pl";
47
44
48
my $dbh = C4::Context->dbh;
45
my $dbh = C4::Context->dbh;
49
my $cache = Koha::Caches->get_instance();
46
my $cache = C4::Context->cache;
50
47
51
# open template
48
# open template
52
my ($template, $loggedinuser, $cookie)
49
my ($template, $loggedinuser, $cookie)
(-)a/opac/svc/report (-3 / +2 lines)
Lines 23-34 Link Here
23
23
24
use Modern::Perl;
24
use Modern::Perl;
25
25
26
use C4::Context;
26
use C4::Reports::Guided;
27
use C4::Reports::Guided;
27
use JSON;
28
use JSON;
28
use CGI qw ( -utf8 );
29
use CGI qw ( -utf8 );
29
30
30
use Koha::Caches;
31
32
my $query       = CGI->new();
31
my $query       = CGI->new();
33
my $report_id   = $query->param('id');
32
my $report_id   = $query->param('id');
34
my $report_name = $query->param('name');
33
my $report_name = $query->param('name');
Lines 41-47 die "Sorry this report is not public\n" unless $report_rec->{public}; Link Here
41
40
42
my @sql_params  = $query->param('sql_params');
41
my @sql_params  = $query->param('sql_params');
43
42
44
my $cache = Koha::Caches->get_instance();
43
my $cache = C4::Context->cache;
45
my $cache_active = $cache->is_cache_active;
44
my $cache_active = $cache->is_cache_active;
46
my ($cache_key, $json_text);
45
my ($cache_key, $json_text);
47
if ($cache_active) {
46
if ($cache_active) {
(-)a/reports/guided_reports.pl (-2 / +1 lines)
Lines 30-36 use C4::Output; Link Here
30
use C4::Debug;
30
use C4::Debug;
31
use C4::Koha qw/GetFrameworksLoop/;
31
use C4::Koha qw/GetFrameworksLoop/;
32
use C4::Context;
32
use C4::Context;
33
use Koha::Caches;
34
use C4::Log;
33
use C4::Log;
35
use Koha::DateUtils qw/dt_from_string output_pref/;
34
use Koha::DateUtils qw/dt_from_string output_pref/;
36
use Koha::AuthorisedValue;
35
use Koha::AuthorisedValue;
Lines 49-55 Script to control the guided report creation Link Here
49
=cut
48
=cut
50
49
51
my $input = new CGI;
50
my $input = new CGI;
52
my $usecache = Koha::Caches->get_instance->memcached_cache;
51
my $usecache = C4::Context->cache->memcached_cache;
53
52
54
my $phase = $input->param('phase') // '';
53
my $phase = $input->param('phase') // '';
55
my $flagsrequired;
54
my $flagsrequired;
(-)a/svc/report (-2 / +2 lines)
Lines 25-31 use C4::Reports::Guided; Link Here
25
use JSON;
25
use JSON;
26
use CGI qw ( -utf8 );
26
use CGI qw ( -utf8 );
27
27
28
use Koha::Caches;
28
use C4::Context;
29
29
30
30
31
my $query  = CGI->new();
31
my $query  = CGI->new();
Lines 48-54 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( Link Here
48
    }
48
    }
49
);
49
);
50
50
51
my $cache = Koha::Caches->get_instance();
51
my $cache = C4::Context->cache;
52
my $cache_active = $cache->is_cache_active;
52
my $cache_active = $cache->is_cache_active;
53
my ($cache_key, $json_text);
53
my ($cache_key, $json_text);
54
if ($cache_active) {
54
if ($cache_active) {
(-)a/t/Calendar.t (-2 / +2 lines)
Lines 22-28 use Test::MockModule; Link Here
22
22
23
use DateTime;
23
use DateTime;
24
use DateTime::Duration;
24
use DateTime::Duration;
25
use Koha::Caches;
25
use C4::Context;
26
use Koha::DateUtils;
26
use Koha::DateUtils;
27
27
28
use Module::Load::Conditional qw/check_install/;
28
use Module::Load::Conditional qw/check_install/;
Lines 89-95 fixtures_ok [ Link Here
89
      ],
89
      ],
90
], "add fixtures";
90
], "add fixtures";
91
91
92
my $cache = Koha::Caches->get_instance();
92
my $cache = C4::Context->cache;
93
$cache->clear_from_cache( 'single_holidays') ;
93
$cache->clear_from_cache( 'single_holidays') ;
94
94
95
# 'MPL' branch is arbitrary, is not used at all but is needed for initialization
95
# 'MPL' branch is arbitrary, is not used at all but is needed for initialization
(-)a/t/Context.t (-1 / +27 lines)
Lines 2-8 Link Here
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use DBI;
4
use DBI;
5
use Test::More tests => 26;
5
use Test::More tests => 29;
6
use Test::MockModule;
6
use Test::MockModule;
7
7
8
BEGIN {
8
BEGIN {
Lines 62-64 is(C4::Context->interface, 'opac', 'interface still opac'); Link Here
62
#Bug 14751
62
#Bug 14751
63
is( C4::Context->interface( 'SiP' ), 'sip', 'interface SiP' );
63
is( C4::Context->interface( 'SiP' ), 'sip', 'interface SiP' );
64
is( C4::Context->interface( 'COMMANDLINE' ), 'commandline', 'interface commandline uc' );
64
is( C4::Context->interface( 'COMMANDLINE' ), 'commandline', 'interface commandline uc' );
65
66
my $DUMMY_KOHA_CONF = "<yazgfs><config><kohasite>TEST</kohasite></config></yazgfs>";
67
my $ctx_a = C4::Context->new($DUMMY_KOHA_CONF, "a");
68
my $ctx_b = C4::Context->new($DUMMY_KOHA_CONF, "b");
69
my $cache_key = "test_C4::Context";
70
71
SKIP: {
72
    skip "No cache", 3 unless $ctx_a->cache->is_cache_active && $ctx_b->cache->is_cache_active;
73
74
    # Light warm up
75
    C4::Context->cache->set_in_cache($cache_key, 'c');
76
    $ctx_a->cache->set_in_cache($cache_key, 'a');
77
    $ctx_b->cache->set_in_cache($cache_key, 'b');
78
    is(C4::Context->cache->get_from_cache($cache_key), 'c', "Correct default cache value");
79
    is($ctx_a->cache->get_from_cache($cache_key), 'a', "Correct cache 'a' value");
80
    is($ctx_b->cache->get_from_cache($cache_key), 'b', "Correct cache 'b' value");
81
82
    # A bit more extravagant
83
    # Cannot run atm, fails due to no database in config
84
#   $ctx_a->run_within_context( sub {
85
#       $ctx_b->cache->set_in_cache($cache_key, 'bb');
86
#       C4::Context->cache->set_in_cache($cache_key, 'aa');
87
#   } );
88
#   is($ctx_a->cache->get_from_cache($cache_key), 'aa', "Correct cache 'a' value");
89
#   is($ctx_b->cache->get_from_cache($cache_key), 'bb', "Correct cache 'b' value");
90
}
(-)a/t/Koha_Template_Plugin_Cache.t (-1 / +3 lines)
Lines 1-6 Link Here
1
use Modern::Perl;
1
use Modern::Perl;
2
use Test::More tests => 2;
2
use Test::More tests => 2;
3
3
4
use C4::Context;
5
4
use_ok('Koha::Template::Plugin::Cache');
6
use_ok('Koha::Template::Plugin::Cache');
5
7
6
ok(my $cache = Koha::Template::Plugin::Cache->new());
8
ok(my $cache = Koha::Template::Plugin::Cache->new(C4::Context->current));
(-)a/t/db_dependent/Context.t (-7 / +1 lines)
Lines 12-25 use Koha::Database; Link Here
12
12
13
BEGIN {
13
BEGIN {
14
    $debug = $ENV{DEBUG} || 0;
14
    $debug = $ENV{DEBUG} || 0;
15
16
    # Note: The overall number of tests may vary by configuration.
17
    # First we need to check your environmental variables
18
    for (qw(KOHA_CONF PERL5LIB)) {
19
        ok( $ret = $ENV{$_}, "ENV{$_} = $ret" );
20
    }
21
    use_ok('C4::Context');
22
}
15
}
16
use_ok('C4::Context');
23
17
24
ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
18
ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
25
19
(-)a/t/db_dependent/Filter_MARC_ViewPolicy.t (-3 / +2 lines)
Lines 30-36 use MARC::Record; Link Here
30
use MARC::Field;
30
use MARC::Field;
31
use C4::Context;
31
use C4::Context;
32
use C4::Biblio;
32
use C4::Biblio;
33
use Koha::Caches;
34
use Koha::Database;
33
use Koha::Database;
35
34
36
BEGIN {
35
BEGIN {
Lines 66-72 sub run_hiding_tests { Link Here
66
65
67
        $sth->execute($hidden_value);
66
        $sth->execute($hidden_value);
68
67
69
        my $cache = Koha::Caches->get_instance();
68
        my $cache = C4::Context->cache;
70
        $cache->flush_all();    # easy way to ensure DB is queried again.
69
        $cache->flush_all();    # easy way to ensure DB is queried again.
71
70
72
        my $processor = Koha::RecordProcessor->new(
71
        my $processor = Koha::RecordProcessor->new(
Lines 138-144 sub run_hiding_tests { Link Here
138
137
139
    $sth->execute(-1); # -1 is visible in opac and intranet.
138
    $sth->execute(-1); # -1 is visible in opac and intranet.
140
139
141
    my $cache = Koha::Caches->get_instance();
140
    my $cache = C4::Context->cache;
142
    $cache->flush_all();    # easy way to ensure DB is queried again.
141
    $cache->flush_all();    # easy way to ensure DB is queried again.
143
142
144
    my $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
143
    my $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
(-)a/t/db_dependent/Items.t (-2 / +3 lines)
Lines 19-24 Link Here
19
use Modern::Perl;
19
use Modern::Perl;
20
20
21
use MARC::Record;
21
use MARC::Record;
22
use C4::Context;
22
use C4::Biblio;
23
use C4::Biblio;
23
use Koha::Database;
24
use Koha::Database;
24
use Koha::Library;
25
use Koha::Library;
Lines 380-386 subtest 'SearchItems test' => sub { Link Here
380
    $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", ?)', undef, $itemfield, $frameworkcode);
381
    $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", ?)', undef, $itemfield, $frameworkcode);
381
382
382
    # Clear cache
383
    # Clear cache
383
    my $cache = Koha::Caches->get_instance();
384
    my $cache = C4::Context->cache;
384
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
385
    $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
385
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
386
    $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
386
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
387
    $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
Lines 644-650 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { Link Here
644
    |, undef, $framework->{frameworkcode} );
645
    |, undef, $framework->{frameworkcode} );
645
646
646
    # And make sure the caches are cleared
647
    # And make sure the caches are cleared
647
    my $cache = Koha::Caches->get_instance();
648
    my $cache = C4::Context->cache;
648
    $cache->clear_from_cache("MarcStructure-0-" . $framework->{frameworkcode});
649
    $cache->clear_from_cache("MarcStructure-0-" . $framework->{frameworkcode});
649
    $cache->clear_from_cache("MarcStructure-1-" . $framework->{frameworkcode});
650
    $cache->clear_from_cache("MarcStructure-1-" . $framework->{frameworkcode});
650
    $cache->clear_from_cache("default_value_for_mod_marc-" . $framework->{frameworkcode});
651
    $cache->clear_from_cache("default_value_for_mod_marc-" . $framework->{frameworkcode});
(-)a/tools/newHolidays.pl (-3 / +2 lines)
Lines 10-16 use CGI qw ( -utf8 ); Link Here
10
use C4::Auth;
10
use C4::Auth;
11
use C4::Output;
11
use C4::Output;
12
12
13
use Koha::Caches;
13
use C4::Context;
14
14
15
use C4::Calendar;
15
use C4::Calendar;
16
use DateTime;
16
use DateTime;
Lines 128-133 sub add_holiday { Link Here
128
        }
128
        }
129
    }
129
    }
130
    # we updated the single_holidays table, so wipe its cache
130
    # we updated the single_holidays table, so wipe its cache
131
    my $cache = Koha::Caches->get_instance();
131
    my $cache = C4::Context->cache;
132
    $cache->clear_from_cache( 'single_holidays') ;
132
    $cache->clear_from_cache( 'single_holidays') ;
133
}
133
}
134
- 

Return to bug 15562