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

(-)a/C4/Biblio.pm (-2 / +2 lines)
Lines 37-44 use C4::ClassSource; Link Here
37
use C4::Charset;
37
use C4::Charset;
38
use C4::Linker;
38
use C4::Linker;
39
use C4::OAI::Sets;
39
use C4::OAI::Sets;
40
use C4::Context;
40
41
41
use Koha::Cache;
42
use Koha::Authority::Types;
42
use Koha::Authority::Types;
43
use Koha::Acquisition::Currencies;
43
use Koha::Acquisition::Currencies;
44
44
Lines 1119-1125 sub GetMarcStructure { Link Here
1119
    $frameworkcode = "" unless $frameworkcode;
1119
    $frameworkcode = "" unless $frameworkcode;
1120
1120
1121
    $forlibrarian = $forlibrarian ? 1 : 0;
1121
    $forlibrarian = $forlibrarian ? 1 : 0;
1122
    my $cache = Koha::Cache->get_instance();
1122
    my $cache = C4::Context->cache;
1123
    my $cache_key = "MarcStructure-$forlibrarian-$frameworkcode";
1123
    my $cache_key = "MarcStructure-$forlibrarian-$frameworkcode";
1124
    my $cached = $cache->get_from_cache($cache_key);
1124
    my $cached = $cache->get_from_cache($cache_key);
1125
    return $cached if $cached;
1125
    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::Cache;
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::Cache->get_instance();
278
    my $cache = C4::Context->cache;
280
    $cache->clear_from_cache( 'single_holidays') ;
279
    $cache->clear_from_cache( 'single_holidays') ;
281
280
282
    return $self;
281
    return $self;
Lines 320-326 sub insert_exception_holiday { Link Here
320
    $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
319
    $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
321
320
322
    # changed the 'single_holidays' table, lets force/reset its cache
321
    # changed the 'single_holidays' table, lets force/reset its cache
323
    my $cache = Koha::Cache->get_instance();
322
    my $cache = C4::Context->cache;
324
    $cache->clear_from_cache( 'single_holidays') ;
323
    $cache->clear_from_cache( 'single_holidays') ;
325
324
326
    return $self;
325
    return $self;
Lines 420-426 UPDATE special_holidays SET title = ?, description = ? Link Here
420
    $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
419
    $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
421
420
422
    # changed the 'single_holidays' table, lets force/reset its cache
421
    # changed the 'single_holidays' table, lets force/reset its cache
423
    my $cache = Koha::Cache->get_instance();
422
    my $cache = C4::Context->cache;
424
    $cache->clear_from_cache( 'single_holidays') ;
423
    $cache->clear_from_cache( 'single_holidays') ;
425
424
426
    return $self;
425
    return $self;
Lines 462-468 UPDATE special_holidays SET title = ?, description = ? Link Here
462
    $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
461
    $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
463
462
464
    # changed the 'single_holidays' table, lets force/reset its cache
463
    # changed the 'single_holidays' table, lets force/reset its cache
465
    my $cache = Koha::Cache->get_instance();
464
    my $cache = C4::Context->cache;
466
    $cache->clear_from_cache( 'single_holidays') ;
465
    $cache->clear_from_cache( 'single_holidays') ;
467
466
468
    return $self;
467
    return $self;
Lines 542-548 sub delete_holiday { Link Here
542
    }
541
    }
543
542
544
    # changed the 'single_holidays' table, lets force/reset its cache
543
    # changed the 'single_holidays' table, lets force/reset its cache
545
    my $cache = Koha::Cache->get_instance();
544
    my $cache = C4::Context->cache;
546
    $cache->clear_from_cache( 'single_holidays') ;
545
    $cache->clear_from_cache( 'single_holidays') ;
547
546
548
    return $self;
547
    return $self;
Lines 572-578 sub delete_holiday_range { Link Here
572
    $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
571
    $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
573
572
574
    # changed the 'single_holidays' table, lets force/reset its cache
573
    # changed the 'single_holidays' table, lets force/reset its cache
575
    my $cache = Koha::Cache->get_instance();
574
    my $cache = C4::Context->cache;
576
    $cache->clear_from_cache( 'single_holidays') ;
575
    $cache->clear_from_cache( 'single_holidays') ;
577
576
578
}
577
}
Lines 625-631 sub delete_exception_holiday_range { Link Here
625
    $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
624
    $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
626
625
627
    # changed the 'single_holidays' table, lets force/reset its cache
626
    # changed the 'single_holidays' table, lets force/reset its cache
628
    my $cache = Koha::Cache->get_instance();
627
    my $cache = C4::Context->cache;
629
    $cache->clear_from_cache( 'single_holidays') ;
628
    $cache->clear_from_cache( 'single_holidays') ;
630
}
629
}
631
630
(-)a/C4/Context.pm (-49 / +75 lines)
Lines 18-24 package C4::Context; Link Here
18
18
19
use strict;
19
use strict;
20
use warnings;
20
use warnings;
21
use vars qw($VERSION $AUTOLOAD $context @context_stack $servers $memcached $ismemcached);
21
use vars qw($VERSION $AUTOLOAD $context @context_stack $memcached_servers);
22
BEGIN {
22
BEGIN {
23
	if ($ENV{'HTTP_USER_AGENT'})	{
23
	if ($ENV{'HTTP_USER_AGENT'})	{
24
		require CGI::Carp;
24
		require CGI::Carp;
Lines 79-98 BEGIN { Link Here
79
    }  	# else there is no browser to send fatals to!
79
    }  	# else there is no browser to send fatals to!
80
80
81
    # Check if there are memcached servers set
81
    # Check if there are memcached servers set
82
    $servers = $ENV{'MEMCACHED_SERVERS'};
82
    $memcached_servers = $ENV{'MEMCACHED_SERVERS'};
83
    if ($servers) {
83
    # Load required libraries and create the memcached object
84
        # Load required libraries and create the memcached object
84
    require Cache::Memcached if $memcached_servers;
85
        require Cache::Memcached;
86
        $memcached = Cache::Memcached->new({
87
        servers => [ $servers ],
88
        debug   => 0,
89
        compress_threshold => 10_000,
90
        expire_time => 600,
91
        namespace => $ENV{'MEMCACHED_NAMESPACE'} || 'koha'
92
    });
93
        # Verify memcached available (set a variable and test the output)
94
    $ismemcached = $memcached->set('ismemcached','1');
95
    }
96
85
97
    $VERSION = '3.07.00.049';
86
    $VERSION = '3.07.00.049';
98
}
87
}
Lines 211-239 sub current { Link Here
211
    return $context;
200
    return $context;
212
}
201
}
213
202
214
=head2 ismemcached
203
sub _new_memcached {
204
    my $namespace = shift or die "No memcached namespace";
215
205
216
Returns the value of the $ismemcached variable (0/1)
206
    return unless $memcached_servers;
207
    return Cache::Memcached->new({
208
        servers => [ $memcached_servers ],
209
        debug   => 0,
210
        compress_threshold => 10_000,
211
        expire_time => 600,
212
        namespace => $namespace
213
    });
214
}
215
# Verify memcached available (test the output)
216
sub _ping_memcached {
217
    my $memcached = shift or croak "No memcached";
218
219
    return $memcached->set('ismemcached','1');
220
}
221
222
=head2 cache
223
224
Returns the cache object or undef
217
225
218
=cut
226
=cut
219
227
220
sub ismemcached {
228
sub cache {
221
    return $ismemcached;
229
    my $self = shift;
230
231
    $self = $context unless ref ($self);
232
    return unless $self;
233
234
    return $self->{cache} ||= Koha::Cache->new();
222
}
235
}
223
236
224
=head2 memcached
237
=head2 memcached
225
238
226
If $ismemcached is true, returns the $memcache variable.
239
Returns the memcached object or undef
227
Returns undef otherwise
240
241
=head2 ismemcached
228
242
229
=cut
243
=cut
230
244
231
sub memcached {
245
sub memcached {
232
    if ($ismemcached) {
246
    my $self = shift;
233
      return $memcached;
247
234
    } else {
248
    $self = $context unless ref ($self);
235
      return;
249
    return unless $self;
236
    }
250
251
    my $memcached = $self->{memcached} or return;
252
    return _ping_memcached($memcached) ? $memcached : undef;
253
}
254
255
sub ismemcached {
256
    my $self = shift;
257
    return $self->memcached;
237
}
258
}
238
259
239
sub db_driver {
260
sub db_driver {
Lines 277-286 sub import { Link Here
277
    # default context already exists?
298
    # default context already exists?
278
    return if $context;
299
    return if $context;
279
300
280
    if ($ismemcached) {
301
    return if $config_file && $config_file eq ":no_config";
302
303
    my $memcached = _new_memcached($ENV{'MEMCACHED_NAMESPACE'} || 'koha');
304
    if ($memcached) {
281
        # retrieve from memcached
305
        # retrieve from memcached
282
        if (my $self = $memcached->get('kohaconf')) {
306
        if ($context = $memcached->get('kohaconf')) {
283
            $context = $self;
307
            $context->{memcached} = $memcached;
284
            return;
308
            return;
285
        }
309
        }
286
    }
310
    }
Lines 307-321 sub import { Link Here
307
    }
331
    }
308
332
309
    # no ? so load it!
333
    # no ? so load it!
310
    return if $config_file && $config_file eq ":no_config";
334
    $context = $pkg->new($config_file) or return;
311
    my $new_ctx = __PACKAGE__->new($config_file);
335
    if ( $memcached && _ping_memcached($memcached) ) {
312
    return unless $new_ctx;
336
        $context->{memcached} = $memcached;
313
337
        $memcached->set('kohaconf',$context);
314
    # if successfully loaded, use it by default
315
    $context = $new_ctx;
316
317
    if ($ismemcached) {
318
      $memcached->set('kohaconf',$new_ctx);
319
    }
338
    }
320
}
339
}
321
340
Lines 357-362 sub new { Link Here
357
    my $class = shift;
376
    my $class = shift;
358
    my $conf_fname = shift or croak "No conf";
377
    my $conf_fname = shift or croak "No conf";
359
    my $namespace = shift;
378
    my $namespace = shift;
379
    my $cache = shift;
360
380
361
    my $self = XMLin(
381
    my $self = XMLin(
362
        $conf_fname,
382
        $conf_fname,
Lines 368-374 sub new { Link Here
368
      unless ref($self) && $self->{"config"};
388
      unless ref($self) && $self->{"config"};
369
389
370
    $self->{"config_file"} = $conf_fname;
390
    $self->{"config_file"} = $conf_fname;
371
    $self->{"namespace"} = $namespace;
391
    if ($namespace) {
392
        $self->{namespace} = $namespace;
393
        my $memcached = $context->{memcached};
394
        $self->{memcached} = $memcached && $memcached->{namespace} eq $namespace
395
            ? $memcached
396
            : _new_memcached($namespace);
397
        # Koha::Cache is far too complex to try to make any savings
398
        $cache ||= Koha::Cache->new( {namespace => $namespace} );
399
    }
400
    $self->{cache} = $cache;
372
401
373
    $self->{"Zconn"} = undef;    # Zebra Connections
402
    $self->{"Zconn"} = undef;    # Zebra Connections
374
    $self->{"marcfromkohafield"} = undef; # the hash with relations between koha table fields and MARC field/subfield
403
    $self->{"marcfromkohafield"} = undef; # the hash with relations between koha table fields and MARC field/subfield
Lines 428-435 sub set_context Link Here
428
        $schema = $self->{schema} ||= Koha::Database->new_schema($self);
457
        $schema = $self->{schema} ||= Koha::Database->new_schema($self);
429
    }
458
    }
430
459
431
    # Save the old context, if any, on the stack
460
    # Save the old context on the stack
432
    push @context_stack, $context if defined($context);
461
    push @context_stack, $context;
433
462
434
    # Set the new context
463
    # Set the new context
435
    $context = $new_context;
464
    $context = $new_context;
Lines 541-547 with this method. Link Here
541
570
542
=cut
571
=cut
543
572
544
my $syspref_cache = Koha::Cache->get_instance();
545
my $use_syspref_cache = 1;
573
my $use_syspref_cache = 1;
546
sub preference {
574
sub preference {
547
    my $self = shift;
575
    my $self = shift;
Lines 554-560 sub preference { Link Here
554
    return $self->{sysprefs}{$var} if $use_syspref_cache && $self && exists $self->{sysprefs}{$var};
582
    return $self->{sysprefs}{$var} if $use_syspref_cache && $self && exists $self->{sysprefs}{$var};
555
583
556
    my $cached_var = $use_syspref_cache
584
    my $cached_var = $use_syspref_cache
557
        ? $syspref_cache->get_from_cache("syspref_$var")
585
        ? $self->cache->get_from_cache("syspref_$var")
558
        : undef;
586
        : undef;
559
    return $cached_var if defined $cached_var;
587
    return $cached_var if defined $cached_var;
560
588
Lines 566-572 sub preference { Link Here
566
    }
594
    }
567
595
568
    if ( $use_syspref_cache ) {
596
    if ( $use_syspref_cache ) {
569
        $syspref_cache->set_in_cache("syspref_$var", $value);
597
        $self->cache->set_in_cache("syspref_$var", $value);
570
        $self->{sysprefs}{$var} = $value if $self;
598
        $self->{sysprefs}{$var} = $value if $self;
571
    }
599
    }
572
    return $value;
600
    return $value;
Lines 625-633 sub clear_syspref_cache { Link Here
625
653
626
    $self = $context unless ref $self;
654
    $self = $context unless ref $self;
627
655
628
    return unless $use_syspref_cache;
656
    $self->cache->flush_all;
629
630
    $syspref_cache->flush_all;
631
    $self->{sysprefs} = {} if $self;
657
    $self->{sysprefs} = {} if $self;
632
}
658
}
633
659
Lines 682-688 sub set_preference { Link Here
682
    }
708
    }
683
709
684
    if ( $use_syspref_cache ) {
710
    if ( $use_syspref_cache ) {
685
        $syspref_cache->set_in_cache( "syspref_$variable", $value );
711
        $self->cache->set_in_cache( "syspref_$variable", $value );
686
        $self->{sysprefs}{$variable} = $value if $self;
712
        $self->{sysprefs}{$variable} = $value if $self;
687
    }
713
    }
688
714
Lines 706-712 sub delete_preference { Link Here
706
732
707
    if ( Koha::Config::SysPrefs->find( $var )->delete ) {
733
    if ( Koha::Config::SysPrefs->find( $var )->delete ) {
708
        if ( $use_syspref_cache ) {
734
        if ( $use_syspref_cache ) {
709
            $syspref_cache->clear_from_cache("syspref_$var");
735
            $self->cache->clear_from_cache("syspref_$var");
710
            delete $self->{sysprefs}{$var} if $self;
736
            delete $self->{sysprefs}{$var} if $self;
711
        }
737
        }
712
738
(-)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::Cache;
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 98-106 sub GetOverDriveToken { Link Here
98
98
99
    return unless ( $key && $secret ) ;
99
    return unless ( $key && $secret ) ;
100
100
101
    my $cache;
101
    my $cache = C4::Context->cache;
102
103
    eval { $cache = Koha::Cache->get_instance() };
104
102
105
    my $token;
103
    my $token;
106
    $cache and $token = $cache->get_from_cache( "overdrive_token" ) and return $token;
104
    $cache and $token = $cache->get_from_cache( "overdrive_token" ) and return $token;
(-)a/C4/Koha.pm (-2 / +1 lines)
Lines 25-31 use strict; Link Here
25
25
26
use C4::Context;
26
use C4::Context;
27
use C4::Branch; # Can be removed?
27
use C4::Branch; # Can be removed?
28
use Koha::Cache;
29
use Koha::DateUtils qw(dt_from_string);
28
use Koha::DateUtils qw(dt_from_string);
30
use Koha::Libraries;
29
use Koha::Libraries;
31
use DateTime::Format::MySQL;
30
use DateTime::Format::MySQL;
Lines 1028-1034 sub GetAuthorisedValues { Link Here
1028
    my $selected_key = defined($selected) ? $selected : '';
1027
    my $selected_key = defined($selected) ? $selected : '';
1029
    my $cache_key =
1028
    my $cache_key =
1030
      "AuthorisedValues-$category-$selected_key-$opac-$branch_limit";
1029
      "AuthorisedValues-$category-$selected_key-$opac-$branch_limit";
1031
    my $cache  = Koha::Cache->get_instance();
1030
    my $cache  = C4::Context->cache;
1032
    my $result = $cache->get_from_cache($cache_key);
1031
    my $result = $cache->get_from_cache($cache_key);
1033
    return $result if $result;
1032
    return $result if $result;
1034
1033
(-)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::Cache;
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::Cache->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 (-17 lines)
Lines 46-68 use base qw(Class::Accessor); Link Here
46
__PACKAGE__->mk_ro_accessors(
46
__PACKAGE__->mk_ro_accessors(
47
    qw( cache memcached_cache fastmmap_cache memory_cache ));
47
    qw( cache memcached_cache fastmmap_cache memory_cache ));
48
48
49
=head2 get_instance
50
51
    my $cache = Koha::Cache->get_instance();
52
53
This gets a shared instance of the cache, set up in a very default way. This is
54
the recommended way to fetch a cache object. If possible, it'll be
55
persistent across multiple instances.
56
57
=cut
58
59
our $singleton_cache;
60
sub get_instance {
61
    my ($class) = @_;
62
    $singleton_cache = $class->new() unless $singleton_cache;
63
    return $singleton_cache;
64
}
65
66
=head2 new
49
=head2 new
67
50
68
Create a new Koha::Cache object. This is required for all cache-related functionality.
51
Create a new Koha::Cache object. This is required for all cache-related functionality.
(-)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::Cache;
11
use Carp;
10
use Carp;
12
11
13
sub new {
12
sub new {
Lines 57-63 sub _init { Link Here
57
# lists breaks persistance engines.  As of 2013-12-10, the RM
56
# lists breaks persistance engines.  As of 2013-12-10, the RM
58
# is allowing this with the expectation that prior to release of
57
# is allowing this with the expectation that prior to release of
59
# 3.16, bug 8089 will be fixed and we can switch the caching over
58
# 3.16, bug 8089 will be fixed and we can switch the caching over
60
# to Koha::Cache.
59
# to external cache
61
60
62
our $exception_holidays;
61
our $exception_holidays;
63
62
Lines 92-98 sub exception_holidays { Link Here
92
sub single_holidays {
91
sub single_holidays {
93
    my ( $self, $date ) = @_;
92
    my ( $self, $date ) = @_;
94
    my $branchcode = $self->{branchcode};
93
    my $branchcode = $self->{branchcode};
95
    my $cache           = Koha::Cache->get_instance();
94
    my $cache           = C4::Context->cache;
96
    my $single_holidays = $cache->get_from_cache('single_holidays');
95
    my $single_holidays = $cache->get_from_cache('single_holidays');
97
96
98
    # $single_holidays looks like:
97
    # $single_holidays looks like:
(-)a/Koha/Handler/Plack.pm (-2 / +39 lines)
Lines 69-74 use Plack::Builder; 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::Builder; 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::Builder; 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_SHARED_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_SHARED_PROPERTIES) {
162
                    # Clean slate
163
                    $cache->delete($_);
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
            $builder->mount("http://$host/" => sub {
172
            $builder->mount("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_SHARED_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_SHARED_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/Template/Plugin/Cache.pm (-3 / +1 lines)
Lines 22-28 use warnings; Link Here
22
use vars qw( $VERSION );
22
use vars qw( $VERSION );
23
use base qw( Template::Plugin );
23
use base qw( Template::Plugin );
24
use Template::Plugin;
24
use Template::Plugin;
25
use C4::Context;
26
$VERSION = '0.01';
25
$VERSION = '0.01';
27
26
28
#------------------------------------------------------------------------
27
#------------------------------------------------------------------------
Lines 36-43 sub new { Link Here
36
        $cache = delete $params->{cache};
35
        $cache = delete $params->{cache};
37
    }
36
    }
38
    else {
37
    else {
39
        require Koha::Cache;
38
        $cache = $context->cache;
40
        $cache = Koha::Cache->get_instance();
41
    }
39
    }
42
    my $self = bless {
40
    my $self = bless {
43
        CACHE   => $cache,
41
        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::Cache;
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::Cache->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::Cache->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 78-84 my ( $template, $borrowernumber, $cookie ) = get_template_and_user( Link Here
78
        debug           => 1,
78
        debug           => 1,
79
    }
79
    }
80
);
80
);
81
my $cache = Koha::Cache->get_instance();
81
my $cache = C4::Context->cache;
82
82
83
my $op       = $input->param('op');
83
my $op       = $input->param('op');
84
$tagfield =~ s/\,//g;
84
$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::Cache;
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::Cache->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::Cache;
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::Cache->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/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::Cache;
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::Cache->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/Cache.t (-2 / +2 lines)
Lines 31-37 SKIP: { Link Here
31
    # Set a special namespace for testing, to avoid breaking
31
    # Set a special namespace for testing, to avoid breaking
32
    # if test is run with a different user than Apache's.
32
    # if test is run with a different user than Apache's.
33
    $ENV{ MEMCACHED_NAMESPACE } = 'unit_tests';
33
    $ENV{ MEMCACHED_NAMESPACE } = 'unit_tests';
34
    my $cache = Koha::Cache->get_instance();
34
    my $cache = C4::Context->cache;
35
35
36
    skip "Cache not enabled", 28
36
    skip "Cache not enabled", 28
37
      unless ( $cache->is_cache_active() && defined $cache );
37
      unless ( $cache->is_cache_active() && defined $cache );
Lines 167-173 SKIP: { Link Here
167
END {
167
END {
168
  SKIP: {
168
  SKIP: {
169
        $ENV{ MEMCACHED_NAMESPACE } = 'unit_tests';
169
        $ENV{ MEMCACHED_NAMESPACE } = 'unit_tests';
170
        my $cache = Koha::Cache->get_instance();
170
        my $cache = C4::Context->cache;
171
        skip "Cache not enabled", 1
171
        skip "Cache not enabled", 1
172
          unless ( $cache->is_cache_active() );
172
          unless ( $cache->is_cache_active() );
173
        is( $destructorcount, 1, 'Destructor run exactly once' );
173
        is( $destructorcount, 1, 'Destructor run exactly once' );
(-)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::Cache;
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 88-94 fixtures_ok [ Link Here
88
      ],
88
      ],
89
], "add fixtures";
89
], "add fixtures";
90
90
91
my $cache = Koha::Cache->get_instance();
91
my $cache = C4::Context->cache;
92
$cache->clear_from_cache( 'single_holidays') ;
92
$cache->clear_from_cache( 'single_holidays') ;
93
93
94
# 'MPL' branch is arbitrary, is not used at all but is needed for initialization
94
# '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 / +3 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
Lines 97-102 ok($trace_read, 'Retrieved syspref from database'); Link Here
97
$trace_read = q{};
91
$trace_read = q{};
98
92
99
C4::Context->enable_syspref_cache();
93
C4::Context->enable_syspref_cache();
94
C4::Context->preference("SillyPreference");
95
$trace_read = q{};
100
is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
96
is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
101
isnt( $trace_read, q{}, 'The pref should be retrieved from the database if the cache has been enabled');
97
isnt( $trace_read, q{}, 'The pref should be retrieved from the database if the cache has been enabled');
102
$trace_read = q{};
98
$trace_read = q{};
(-)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::Cache;
13
use C4::Context;
14
14
15
use C4::Calendar;
15
use C4::Calendar;
16
use DateTime;
16
use DateTime;
Lines 129-134 sub add_holiday { Link Here
129
        }
129
        }
130
    }
130
    }
131
    # we updated the single_holidays table, so wipe its cache
131
    # we updated the single_holidays table, so wipe its cache
132
    my $cache = Koha::Cache->get_instance();
132
    my $cache = C4::Context->cache;
133
    $cache->clear_from_cache( 'single_holidays') ;
133
    $cache->clear_from_cache( 'single_holidays') ;
134
}
134
}
135
- 

Return to bug 15562