From 4bc409af8a4c91015c3e22b16d67d9a04dd3999e Mon Sep 17 00:00:00 2001 From: Srdjan Date: Wed, 27 Jan 2016 16:57:18 +1300 Subject: [PATCH] bug_15562: Removed Koha::Cache->get_instance() There should be no cache singleton, full stop. If Koha is to move away from .pl scripts that is. As an interim measure Koha::Cache->get_instance() is replaced with C4::Context->cache, in the vein of C4::Context->memcached. In that respect it will continue to work in the singleton-ish way if context is used as a singleton, but supports cache-per-context. Koha::Handler::Plack->app_per_host() cache sysprefs using Context memcached. https://bugs.koha-community.org/show_bug.cgi?id=15562 --- C4/Biblio.pm | 6 +- C4/Calendar.pm | 15 ++-- C4/Context.pm | 123 ++++++++++++++++++++------------ C4/External/OverDrive.pm | 6 +- C4/Items.pm | 2 +- C4/Koha.pm | 3 +- C4/Utils/DataTables/ColumnsSettings.pm | 3 +- Koha/Cache.pm | 17 ----- Koha/Calendar.pm | 5 +- Koha/Handler/Plack.pm | 41 ++++++++++- Koha/Handler/Plack/CGI.pm | 2 +- Koha/Template/Plugin/Cache.pm | 3 +- admin/biblio_framework.pl | 3 +- admin/koha2marclinks.pl | 2 +- admin/marc_subfields_structure.pl | 2 +- admin/marctagstructure.pl | 5 +- opac/svc/report | 5 +- svc/report | 4 +- t/Cache.t | 4 +- t/Calendar.t | 4 +- t/Context.t | 28 +++++++- t/Koha_Template_Plugin_Cache.t | 4 +- t/db_dependent/Context.t | 8 +-- t/db_dependent/Filter_MARC_ViewPolicy.t | 2 +- t/db_dependent/Items.t | 5 +- tools/newHolidays.pl | 4 +- 26 files changed, 185 insertions(+), 121 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 2798325..243cfb0 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -38,8 +38,8 @@ use C4::Charset; use C4::Linker; use C4::OAI::Sets; use C4::Debug; +use C4::Context; -use Koha::Cache; use Koha::Authority::Types; use Koha::Acquisition::Currencies; use Koha::SearchEngine; @@ -1122,7 +1122,7 @@ sub GetMarcStructure { $frameworkcode = "" unless $frameworkcode; $forlibrarian = $forlibrarian ? 1 : 0; - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; my $cache_key = "MarcStructure-$forlibrarian-$frameworkcode"; my $cached = $cache->get_from_cache($cache_key); return $cached if $cached; @@ -1230,7 +1230,7 @@ sub GetMarcSubfieldStructure { $frameworkcode //= ''; - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; my $cache_key = "MarcSubfieldStructure-$frameworkcode"; my $cached = $cache->get_from_cache($cache_key); return $cached if $cached; diff --git a/C4/Calendar.pm b/C4/Calendar.pm index 82dc35a..3a38173 100644 --- a/C4/Calendar.pm +++ b/C4/Calendar.pm @@ -23,7 +23,6 @@ use Carp; use Date::Calc qw( Date_to_Days Today); use C4::Context; -use Koha::Cache; use constant ISO_DATE_FORMAT => "%04d-%02d-%02d"; @@ -276,7 +275,7 @@ sub insert_single_holiday { # changed the 'single_holidays' table, lets force/reset its cache - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; $cache->clear_from_cache( 'single_holidays') ; $cache->clear_from_cache( 'exception_holidays') ; @@ -321,7 +320,7 @@ sub insert_exception_holiday { $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; # changed the 'single_holidays' table, lets force/reset its cache - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; $cache->clear_from_cache( 'single_holidays') ; $cache->clear_from_cache( 'exception_holidays') ; @@ -422,7 +421,7 @@ UPDATE special_holidays SET title = ?, description = ? $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; # changed the 'single_holidays' table, lets force/reset its cache - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; $cache->clear_from_cache( 'single_holidays') ; $cache->clear_from_cache( 'exception_holidays') ; @@ -465,7 +464,7 @@ UPDATE special_holidays SET title = ?, description = ? $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; # changed the 'single_holidays' table, lets force/reset its cache - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; $cache->clear_from_cache( 'single_holidays') ; $cache->clear_from_cache( 'exception_holidays') ; @@ -546,7 +545,7 @@ sub delete_holiday { } # changed the 'single_holidays' table, lets force/reset its cache - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; $cache->clear_from_cache( 'single_holidays') ; $cache->clear_from_cache( 'exception_holidays') ; @@ -577,7 +576,7 @@ sub delete_holiday_range { $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year}); # changed the 'single_holidays' table, lets force/reset its cache - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; $cache->clear_from_cache( 'single_holidays') ; $cache->clear_from_cache( 'exception_holidays') ; @@ -631,7 +630,7 @@ sub delete_exception_holiday_range { $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year}); # changed the 'single_holidays' table, lets force/reset its cache - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; $cache->clear_from_cache( 'single_holidays') ; $cache->clear_from_cache( 'exception_holidays') ; } diff --git a/C4/Context.pm b/C4/Context.pm index 4d4b048..6301223 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -18,7 +18,7 @@ package C4::Context; use strict; use warnings; -use vars qw($AUTOLOAD $context @context_stack $servers $memcached $ismemcached); +use vars qw($AUTOLOAD $context @context_stack $memcached_servers); BEGIN { if ($ENV{'HTTP_USER_AGENT'}) { require CGI::Carp; @@ -88,20 +88,9 @@ BEGIN { } # else there is no browser to send fatals to! # Check if there are memcached servers set - $servers = $ENV{'MEMCACHED_SERVERS'}; - if ($servers) { - # Load required libraries and create the memcached object - require Cache::Memcached; - $memcached = Cache::Memcached->new({ - servers => [ $servers ], - debug => 0, - compress_threshold => 10_000, - expire_time => 600, - namespace => $ENV{'MEMCACHED_NAMESPACE'} || 'koha' - }); - # Verify memcached available (set a variable and test the output) - $ismemcached = $memcached->set('ismemcached','1'); - } + $memcached_servers = $ENV{'MEMCACHED_SERVERS'}; + # Load required libraries and create the memcached object + require Cache::Memcached if $memcached_servers; } @@ -219,29 +208,57 @@ sub current { return $context; } -=head2 ismemcached +sub _new_memcached { + my $namespace = shift or die "No memcached namespace"; + + return unless $memcached_servers; + return Cache::Memcached->new({ + servers => [ $memcached_servers ], + debug => 0, + compress_threshold => 10_000, + expire_time => 600, + namespace => $namespace || $ENV{'MEMCACHED_NAMESPACE'} || 'koha' + }); +} +# Verify memcached available (test the output) +sub _ping_memcached { + my $memcached = shift or croak "No memcached"; -Returns the value of the $ismemcached variable (0/1) + return $memcached->set('ismemcached','1'); +} + +=head2 cache + +Returns the cache object or undef =cut -sub ismemcached { - return $ismemcached; +sub cache { + my $self = shift; + $self = $context unless ref ($self); + + return $self->{cache}; } =head2 memcached -If $ismemcached is true, returns the $memcache variable. -Returns undef otherwise +Returns the memcached object or undef + +=head2 ismemcached =cut sub memcached { - if ($ismemcached) { - return $memcached; - } else { - return; - } + my $self = shift; + $self = $context unless ref ($self); + + my $memcached = $self->{memcached} or return; + return _ping_memcached($memcached) ? $memcached : undef; +} + +sub ismemcached { + my $self = shift; + return $self->memcached; } sub db_driver { @@ -285,10 +302,14 @@ sub import { # default context already exists? return if $context; - if ($ismemcached) { + return if $config_file && $config_file eq ":no_config"; + + my $memcached = _new_memcached($ENV{'MEMCACHED_NAMESPACE'} || 'koha'); + if ($memcached) { # retrieve from memcached - if (my $self = $memcached->get('kohaconf')) { - $context = $self; + if ($context = $memcached->get('kohaconf')) { + $context->{memcached} = $memcached; + $context->{cache} = Koha::Cache->new({namespace => $context->{namespace}}); return; } } @@ -315,16 +336,13 @@ sub import { } # no ? so load it! - return if $config_file && $config_file eq ":no_config"; - my $new_ctx = __PACKAGE__->new($config_file); - return unless $new_ctx; - - # if successfully loaded, use it by default - $context = $new_ctx; - - if ($ismemcached) { - $memcached->set('kohaconf',$new_ctx); + $context = $pkg->_new($config_file) or return; + if ( $memcached && _ping_memcached($memcached) ) { + $memcached->set('kohaconf',$context); + # Canot serialize cache objects + $context->{memcached} = $memcached; } + $context->{cache} = Koha::Cache->new({namespace => $context->{namespace}}); } use Scalar::Util qw(openhandle); @@ -366,6 +384,21 @@ sub new { my $conf_fname = shift or croak "No conf"; my $namespace = shift; + my $self = $class->_new($conf_fname, $namespace); + + if ($memcached_servers) { + $self->{memcached} = _new_memcached($namespace); + } + $self->{cache} = Koha::Cache->new({namespace => $namespace}); + + return $self; +} + +sub _new { + my $class = shift; + my $conf_fname = shift or croak "No conf"; + my $namespace = shift; + my $self = XMLin( $conf_fname, keyattr => ['id'], @@ -378,7 +411,6 @@ sub new { $self->{config_file} = $conf_fname; $self->{namespace} = $namespace; $self->{use_syspref_cache} = 1; - $self->{syspref_cache} = Koha::Cache->new({namespace => $namespace}); $self->{"Zconn"} = undef; # Zebra Connections $self->{"userenv"} = undef; # User env @@ -558,7 +590,7 @@ sub preference { if defined $ENV{"OVERRIDE_SYSPREF_$var"}; my $cached_var = $self->{use_syspref_cache} - ? $self->{syspref_cache}->get_from_cache("syspref_$var") + ? $self->cache->get_from_cache("syspref_$var") : undef; return $cached_var if defined $cached_var; @@ -567,7 +599,8 @@ sub preference { my $value = $syspref ? $syspref->value() : undef; if ( $self->{use_syspref_cache} ) { - $self->{syspref_cache}->set_in_cache("syspref_$var", $value); + $self->cache->set_in_cache("syspref_$var", $value); + $self->{sysprefs}{$var} = $value if $self; } return $value; } @@ -608,8 +641,8 @@ used with Plack and other persistent environments. sub disable_syspref_cache { my ($self) = @_; $self = $context unless ref $self; - $self->{use_syspref_cache} = 0; $self->clear_syspref_cache(); + $self->{use_syspref_cache} = 0; } =head2 clear_syspref_cache @@ -626,7 +659,7 @@ sub clear_syspref_cache { my ($self) = @_; $self = $context unless ref $self; return unless $self->{use_syspref_cache}; - $self->{syspref_cache}->flush_all; + $self->cache->flush_all; } =head2 set_preference @@ -679,7 +712,7 @@ sub set_preference { } if ( $self->{use_syspref_cache} ) { - $self->{syspref_cache}->set_in_cache( "syspref_$variable", $value ); + $self->cache->set_in_cache( "syspref_$variable", $value ); } return $syspref; @@ -701,7 +734,7 @@ sub delete_preference { if ( Koha::Config::SysPrefs->find( $var )->delete ) { if ( $self->{use_syspref_cache} ) { - $self->{syspref_cache}->clear_from_cache("syspref_$var"); + $self->cache->clear_from_cache("syspref_$var"); } return 1; diff --git a/C4/External/OverDrive.pm b/C4/External/OverDrive.pm index 12135c5..0e71707 100644 --- a/C4/External/OverDrive.pm +++ b/C4/External/OverDrive.pm @@ -22,7 +22,7 @@ use warnings; use Koha; use JSON; -use Koha::Cache; +use C4::Context; use HTTP::Request; use HTTP::Request::Common; use LWP::Authen::Basic; @@ -97,9 +97,7 @@ sub GetOverDriveToken { return unless ( $key && $secret ) ; - my $cache; - - eval { $cache = Koha::Cache->get_instance() }; + my $cache = C4::Context->cache; my $token; $cache and $token = $cache->get_from_cache( "overdrive_token" ) and return $token; diff --git a/C4/Items.pm b/C4/Items.pm index c870655..5280d7e 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -452,7 +452,7 @@ Returns item record sub _build_default_values_for_mod_marc { my ($frameworkcode) = @_; - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; my $cache_key = "default_value_for_mod_marc-$frameworkcode"; my $cached = $cache->get_from_cache($cache_key); return $cached if $cached; diff --git a/C4/Koha.pm b/C4/Koha.pm index 7fe07f1..3a67e7f 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -25,7 +25,6 @@ use strict; use C4::Context; use C4::Branch; # Can be removed? -use Koha::Cache; use Koha::DateUtils qw(dt_from_string); use Koha::Libraries; use DateTime::Format::MySQL; @@ -1017,7 +1016,7 @@ sub GetAuthorisedValues { C4::Context->userenv ? C4::Context->userenv->{"branch"} : ""; my $cache_key = "AuthorisedValues-$category-$opac-$branch_limit"; - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; my $result = $cache->get_from_cache($cache_key); return $result if $result; diff --git a/C4/Utils/DataTables/ColumnsSettings.pm b/C4/Utils/DataTables/ColumnsSettings.pm index a107886..31068b4 100644 --- a/C4/Utils/DataTables/ColumnsSettings.pm +++ b/C4/Utils/DataTables/ColumnsSettings.pm @@ -5,11 +5,10 @@ use List::Util qw( first ); use YAML; use C4::Context; use Koha::Database; -use Koha::Cache; sub get_yaml { my $yml_path = C4::Context->config('intranetdir') . '/admin/columns_settings.yml'; - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; my $yaml = $cache->get_from_cache('ColumnsSettingsYaml'); unless ($yaml) { diff --git a/Koha/Cache.pm b/Koha/Cache.pm index ae280a1..f08653c 100644 --- a/Koha/Cache.pm +++ b/Koha/Cache.pm @@ -49,23 +49,6 @@ __PACKAGE__->mk_ro_accessors( our %L1_cache; -=head2 get_instance - - my $cache = Koha::Cache->get_instance(); - -This gets a shared instance of the cache, set up in a very default way. This is -the recommended way to fetch a cache object. If possible, it'll be -persistent across multiple instances. - -=cut - -our $singleton_cache; -sub get_instance { - my ($class) = @_; - $singleton_cache = $class->new() unless $singleton_cache; - return $singleton_cache; -} - =head2 new Create a new Koha::Cache object. This is required for all cache-related functionality. diff --git a/Koha/Calendar.pm b/Koha/Calendar.pm index 1321621..ada63ee 100644 --- a/Koha/Calendar.pm +++ b/Koha/Calendar.pm @@ -7,7 +7,6 @@ use DateTime; use DateTime::Set; use DateTime::Duration; use C4::Context; -use Koha::Cache; use Carp; sub new { @@ -55,7 +54,7 @@ sub _init { sub exception_holidays { my ( $self ) = @_; - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; my $cached = $cache->get_from_cache('exception_holidays'); return $cached if $cached; @@ -84,7 +83,7 @@ sub exception_holidays { sub single_holidays { my ( $self, $date ) = @_; my $branchcode = $self->{branchcode}; - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; my $single_holidays = $cache->get_from_cache('single_holidays'); # $single_holidays looks like: diff --git a/Koha/Handler/Plack.pm b/Koha/Handler/Plack.pm index af3f6cb..bce66ac 100644 --- a/Koha/Handler/Plack.pm +++ b/Koha/Handler/Plack.pm @@ -69,6 +69,7 @@ use Plack::App::URLMap; hostname => 'koha1.com', app => $app1, context => $context1, + shared_context => 1 }, { hostname => ['koha2.com', 'www.koha2.com'], @@ -78,13 +79,16 @@ use Plack::App::URLMap; ... C is mandatory. + If C is set to true, some Context properties will be preserved across + forked processes. Useful if both OPAC and Intranet apps are served here, so no restart + is needed when Context cached properties cnamge values. Needs memcached. koha.psgi: use Plack::Builder; use Plack::App::CGIBin; - use C4::Context; + use C4::Context ":no_config"; my $opac_app = builder { enable "Plack::Middleware::Static", @@ -137,6 +141,8 @@ use Plack::App::URLMap; =cut +# We cannot store whole Context object, may contain non-serializable things +my @CONTEXT_SHARED_PROPERTIES = qw(sysprefs); sub app_per_host { my $class = shift; my $sites = shift or die "No sites spec"; @@ -148,12 +154,43 @@ sub app_per_host { my $app = $site_params->{app} or croak "No app"; my $context = $site_params->{context} or croak "No Koha Context"; + my $shared_context = $site_params->{shared_context}; + my $cache = $context->memcached; + if ($shared_context) { + if ($cache) { + foreach (@CONTEXT_SHARED_PROPERTIES) { + # Clean slate + $cache->delete($_); + } + } + else { + warn "shared_context works only with memcached"; + } + } foreach my $host (@$hosts) { $map->map("http://$host/" => sub { my $env = shift; - return $context->run_within_context(sub { $app->($env) }); + # may have stopped meanwhile or whatever + my $cache = $context->memcached; + if ($shared_context && $cache) { + foreach (@CONTEXT_SHARED_PROPERTIES) { + if (my $shared = $cache->get($_)) { + $context->{$_} = $shared; + } + } + } + + my $ret = $context->run_within_context(sub { $app->($env) }); + + if ($shared_context && $cache) { + foreach (@CONTEXT_SHARED_PROPERTIES) { + $cache->set($_, $context->{$_}); + } + } + + $ret; }); } } diff --git a/Koha/Handler/Plack/CGI.pm b/Koha/Handler/Plack/CGI.pm index 36c6907..9892562 100644 --- a/Koha/Handler/Plack/CGI.pm +++ b/Koha/Handler/Plack/CGI.pm @@ -73,7 +73,7 @@ use Plack::App::CGIBin; use parent "Koha::Handler::Plack"; -use C4::Context; +use C4::Context ":no_config"; =head1 CLASS METHODS diff --git a/Koha/Template/Plugin/Cache.pm b/Koha/Template/Plugin/Cache.pm index dbb1c82..085f977 100644 --- a/Koha/Template/Plugin/Cache.pm +++ b/Koha/Template/Plugin/Cache.pm @@ -34,8 +34,7 @@ sub new { $cache = delete $params->{cache}; } else { - require Koha::Cache; - $cache = Koha::Cache->get_instance(); + $cache = $context->cache; } my $self = bless { CACHE => $cache, diff --git a/admin/biblio_framework.pl b/admin/biblio_framework.pl index 3aeb0ca..e496bd8 100755 --- a/admin/biblio_framework.pl +++ b/admin/biblio_framework.pl @@ -26,12 +26,11 @@ use C4::Output; use Koha::Biblios; use Koha::BiblioFramework; use Koha::BiblioFrameworks; -use Koha::Cache; my $input = new CGI; my $frameworkcode = $input->param('frameworkcode') || q||; my $op = $input->param('op') || q|list|; -my $cache = Koha::Cache->get_instance(); +my $cache = C4::Context->cache; my @messages; my ( $template, $borrowernumber, $cookie ) = get_template_and_user( diff --git a/admin/koha2marclinks.pl b/admin/koha2marclinks.pl index 9ccca37..68f1e55 100755 --- a/admin/koha2marclinks.pl +++ b/admin/koha2marclinks.pl @@ -59,7 +59,7 @@ else { } my $dbh = C4::Context->dbh; -my $cache = Koha::Cache->get_instance(); +my $cache = C4::Context->cache; ################## ADD_FORM ################################## # called by default. Used to create form to add or modify a record diff --git a/admin/marc_subfields_structure.pl b/admin/marc_subfields_structure.pl index 096b9ec..7f7915a 100755 --- a/admin/marc_subfields_structure.pl +++ b/admin/marc_subfields_structure.pl @@ -77,7 +77,7 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user( debug => 1, } ); -my $cache = Koha::Cache->get_instance(); +my $cache = C4::Context->cache; my $op = $input->param('op') || ""; $tagfield =~ s/\,//g; diff --git a/admin/marctagstructure.pl b/admin/marctagstructure.pl index 27d9592..028a5ce 100755 --- a/admin/marctagstructure.pl +++ b/admin/marctagstructure.pl @@ -25,9 +25,6 @@ use C4::Auth; use C4::Koha; use C4::Context; use C4::Output; -use C4::Context; - -use Koha::Cache; # retrieve parameters my $input = new CGI; @@ -46,7 +43,7 @@ my $pagesize = 20; my $script_name = "/cgi-bin/koha/admin/marctagstructure.pl"; my $dbh = C4::Context->dbh; -my $cache = Koha::Cache->get_instance(); +my $cache = C4::Context->cache; # open template my ($template, $loggedinuser, $cookie) diff --git a/opac/svc/report b/opac/svc/report index bfc84e5..98d2aeb 100755 --- a/opac/svc/report +++ b/opac/svc/report @@ -23,12 +23,11 @@ use Modern::Perl; +use C4::Context; use C4::Reports::Guided; use JSON; use CGI qw ( -utf8 ); -use Koha::Cache; - my $query = CGI->new(); my $report_id = $query->param('id'); my $report_name = $query->param('name'); @@ -41,7 +40,7 @@ die "Sorry this report is not public\n" unless $report_rec->{public}; my @sql_params = $query->param('sql_params'); -my $cache = Koha::Cache->get_instance(); +my $cache = C4::Context->cache; my $cache_active = $cache->is_cache_active; my ($cache_key, $json_text); if ($cache_active) { diff --git a/svc/report b/svc/report index da1b9b3..d380090 100755 --- a/svc/report +++ b/svc/report @@ -25,7 +25,7 @@ use C4::Reports::Guided; use JSON; use CGI qw ( -utf8 ); -use Koha::Cache; +use C4::Context; my $query = CGI->new(); @@ -48,7 +48,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( } ); -my $cache = Koha::Cache->get_instance(); +my $cache = C4::Context->cache; my $cache_active = $cache->is_cache_active; my ($cache_key, $json_text); if ($cache_active) { diff --git a/t/Cache.t b/t/Cache.t index 1a25b77..64c86d8 100644 --- a/t/Cache.t +++ b/t/Cache.t @@ -33,7 +33,7 @@ SKIP: { # Set a special namespace for testing, to avoid breaking # if test is run with a different user than Apache's. $ENV{ MEMCACHED_NAMESPACE } = 'unit_tests'; - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; skip "Cache not enabled", 36 unless ( $cache->is_cache_active() && defined $cache ); @@ -259,7 +259,7 @@ subtest 'Koha::Cache::Memory::Lite' => sub { END { SKIP: { $ENV{ MEMCACHED_NAMESPACE } = 'unit_tests'; - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; skip "Cache not enabled", 1 unless ( $cache->is_cache_active() ); is( $destructorcount, 1, 'Destructor run exactly once' ); diff --git a/t/Calendar.t b/t/Calendar.t index 4d48c02..55f5d89 100755 --- a/t/Calendar.t +++ b/t/Calendar.t @@ -22,7 +22,7 @@ use Test::MockModule; use DateTime; use DateTime::Duration; -use Koha::Cache; +use C4::Context; use Koha::DateUtils; use Module::Load::Conditional qw/check_install/; @@ -89,7 +89,7 @@ fixtures_ok [ ], ], "add fixtures"; -my $cache = Koha::Cache->get_instance(); +my $cache = C4::Context->cache; $cache->clear_from_cache( 'single_holidays') ; # 'MPL' branch is arbitrary, is not used at all but is needed for initialization diff --git a/t/Context.t b/t/Context.t index e2c1825..d737509 100755 --- a/t/Context.t +++ b/t/Context.t @@ -2,7 +2,7 @@ use Modern::Perl; use DBI; -use Test::More tests => 26; +use Test::More tests => 29; use Test::MockModule; BEGIN { @@ -62,3 +62,29 @@ is(C4::Context->interface, 'opac', 'interface still opac'); #Bug 14751 is( C4::Context->interface( 'SiP' ), 'sip', 'interface SiP' ); is( C4::Context->interface( 'COMMANDLINE' ), 'commandline', 'interface commandline uc' ); + +my $DUMMY_KOHA_CONF = "TEST"; +my $ctx_a = C4::Context->new($DUMMY_KOHA_CONF, "a"); +my $ctx_b = C4::Context->new($DUMMY_KOHA_CONF, "b"); +my $cache_key = "test_C4::Context"; + +SKIP: { + skip "No cache", 3 unless $ctx_a->cache->is_cache_active && $ctx_b->cache->is_cache_active; + + # Light warm up + C4::Context->cache->set_in_cache($cache_key, 'c'); + $ctx_a->cache->set_in_cache($cache_key, 'a'); + $ctx_b->cache->set_in_cache($cache_key, 'b'); + is(C4::Context->cache->get_from_cache($cache_key), 'c', "Correct default cache value"); + is($ctx_a->cache->get_from_cache($cache_key), 'a', "Correct cache 'a' value"); + is($ctx_b->cache->get_from_cache($cache_key), 'b', "Correct cache 'b' value"); + + # A bit more extravagant + # Cannot run atm, fails due to no database in config +# $ctx_a->run_within_context( sub { +# $ctx_b->cache->set_in_cache($cache_key, 'bb'); +# C4::Context->cache->set_in_cache($cache_key, 'aa'); +# } ); +# is($ctx_a->cache->get_from_cache($cache_key), 'aa', "Correct cache 'a' value"); +# is($ctx_b->cache->get_from_cache($cache_key), 'bb', "Correct cache 'b' value"); +} diff --git a/t/Koha_Template_Plugin_Cache.t b/t/Koha_Template_Plugin_Cache.t index da20f61..15ee048 100644 --- a/t/Koha_Template_Plugin_Cache.t +++ b/t/Koha_Template_Plugin_Cache.t @@ -1,6 +1,8 @@ use Modern::Perl; use Test::More tests => 2; +use C4::Context; + use_ok('Koha::Template::Plugin::Cache'); -ok(my $cache = Koha::Template::Plugin::Cache->new()); +ok(my $cache = Koha::Template::Plugin::Cache->new(C4::Context->current)); diff --git a/t/db_dependent/Context.t b/t/db_dependent/Context.t index c3f6066..b35f83d 100755 --- a/t/db_dependent/Context.t +++ b/t/db_dependent/Context.t @@ -12,14 +12,8 @@ use Koha::Database; BEGIN { $debug = $ENV{DEBUG} || 0; - - # Note: The overall number of tests may vary by configuration. - # First we need to check your environmental variables - for (qw(KOHA_CONF PERL5LIB)) { - ok( $ret = $ENV{$_}, "ENV{$_} = $ret" ); - } - use_ok('C4::Context'); } +use_ok('C4::Context'); ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context'); diff --git a/t/db_dependent/Filter_MARC_ViewPolicy.t b/t/db_dependent/Filter_MARC_ViewPolicy.t index ba446bc..a0123ec 100644 --- a/t/db_dependent/Filter_MARC_ViewPolicy.t +++ b/t/db_dependent/Filter_MARC_ViewPolicy.t @@ -71,7 +71,7 @@ sub run_hiding_tests { $sth->execute($hidden_value); - my $cache = Koha::Cache->get_instance(); + my $cache = Koha::Cache->new(); $cache->flush_all(); # easy way to ensure DB is queried again. my $processor = Koha::RecordProcessor->new( diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t index b494a70..a927c4c 100755 --- a/t/db_dependent/Items.t +++ b/t/db_dependent/Items.t @@ -19,6 +19,7 @@ use Modern::Perl; use MARC::Record; +use C4::Context; use C4::Biblio; use Koha::Database; use Koha::Library; @@ -380,7 +381,7 @@ subtest 'SearchItems test' => sub { $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", ?)', undef, $itemfield, $frameworkcode); # Clear cache - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; $cache->clear_from_cache("MarcStructure-0-$frameworkcode"); $cache->clear_from_cache("MarcStructure-1-$frameworkcode"); $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode"); @@ -644,7 +645,7 @@ subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { |, undef, $framework->{frameworkcode} ); # And make sure the caches are cleared - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; $cache->clear_from_cache("MarcStructure-0-" . $framework->{frameworkcode}); $cache->clear_from_cache("MarcStructure-1-" . $framework->{frameworkcode}); $cache->clear_from_cache("default_value_for_mod_marc-" . $framework->{frameworkcode}); diff --git a/tools/newHolidays.pl b/tools/newHolidays.pl index eda4c1b..38f290c 100755 --- a/tools/newHolidays.pl +++ b/tools/newHolidays.pl @@ -10,7 +10,7 @@ use CGI qw ( -utf8 ); use C4::Auth; use C4::Output; -use Koha::Cache; +use C4::Context; use C4::Calendar; use DateTime; @@ -129,6 +129,6 @@ sub add_holiday { } } # we updated the single_holidays table, so wipe its cache - my $cache = Koha::Cache->get_instance(); + my $cache = C4::Context->cache; $cache->clear_from_cache( 'single_holidays') ; } -- 2.7.4