From 5b99c3629b6eacb81c469b572d93a4ad3e11afcc Mon Sep 17 00:00:00 2001 From: Srdjan Date: Wed, 27 Jan 2016 16:57:18 +1300 Subject: [PATCH] bug_15562: Removed Koha::Caches->get_instance() There should be no cache singletons, full stop. If Koha is to move away from .pl scripts that is. As an interim measure Koha::Caches->get_instance() is replaced with C4::Context->cache. 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/Auth.pm | 10 ++++---- C4/Biblio.pm | 6 ++--- C4/Calendar.pm | 15 ++++++------ C4/Context.pm | 29 ++++++++++++++++++++++- C4/External/OverDrive.pm | 6 ++--- C4/Items.pm | 2 +- C4/Koha.pm | 3 +-- C4/Utils/DataTables/ColumnsSettings.pm | 3 +-- Koha/Cache.pm | 33 +++++++++++++------------- Koha/Caches.pm | 2 +- Koha/Calendar.pm | 5 ++-- Koha/Handler/Plack.pm | 41 +++++++++++++++++++++++++++++++-- Koha/Handler/Plack/CGI.pm | 2 +- Koha/MetaSearcher.pm | 4 ++-- Koha/Template/Plugin/Cache.pm | 4 +--- about.pl | 3 +-- admin/biblio_framework.pl | 3 +-- admin/koha2marclinks.pl | 2 +- admin/marc_subfields_structure.pl | 2 +- admin/marctagstructure.pl | 7 +----- opac/svc/report | 5 ++-- reports/guided_reports.pl | 3 +-- svc/report | 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 | 5 ++-- t/db_dependent/Items.t | 5 ++-- tools/newHolidays.pl | 4 ++-- 30 files changed, 161 insertions(+), 91 deletions(-) diff --git a/C4/Auth.pm b/C4/Auth.pm index d22e935..c6a42cd 100644 --- a/C4/Auth.pm +++ b/C4/Auth.pm @@ -31,7 +31,6 @@ use C4::Templates; # to get the template use C4::Languages; use C4::Search::History; use Koha; -use Koha::Caches; use Koha::AuthUtils qw(get_script_name hash_password); use Koha::Libraries; use Koha::LibraryCategories; @@ -1739,11 +1738,12 @@ sub get_session { elsif ( $storage_method eq 'Pg' ) { $session = new CGI::Session( "driver:PostgreSQL;serializer:yaml;id:md5", $sessionID, { Handle => $dbh } ); } - elsif ( $storage_method eq 'memcached' && Koha::Caches->get_instance->memcached_cache ) { - my $memcached = Koha::Caches->get_instance()->memcached_cache; - $session = new CGI::Session( "driver:memcached;serializer:yaml;id:md5", $sessionID, { Memcached => $memcached } ); + elsif ( $storage_method eq 'memcached' ) { + if ( my $memcached = C4::Context->cache->memcached_cache ) { + $session = new CGI::Session( "driver:memcached;serializer:yaml;id:md5", $sessionID, { Memcached => $memcached } ); + } } - else { + unless ($session) { # catch all defaults to tmp should work on all systems my $dir = File::Spec->tmpdir; my $instance = C4::Context->config( 'database' ); #actually for packages not exactly the instance name, but generally safer to leave it as it is diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 0ee689d..b755963 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -37,8 +37,8 @@ use C4::Charset; use C4::Linker; use C4::OAI::Sets; use C4::Debug; +use C4::Context; -use Koha::Caches; use Koha::Authority::Types; use Koha::Acquisition::Currencies; use Koha::Biblio::Metadata; @@ -971,7 +971,7 @@ sub GetMarcStructure { $forlibrarian = $forlibrarian ? 1 : 0; my $unsafe = ($params && $params->{unsafe})? 1: 0; - my $cache = Koha::Caches->get_instance(); + my $cache = C4::Context->cache; my $cache_key = "MarcStructure-$forlibrarian-$frameworkcode"; my $cached = $cache->get_from_cache($cache_key, { unsafe => $unsafe }); return $cached if $cached; @@ -1079,7 +1079,7 @@ sub GetMarcSubfieldStructure { $frameworkcode //= ''; - my $cache = Koha::Caches->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 321aad7..42fe20b 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::Caches; 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::Caches->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::Caches->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::Caches->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::Caches->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::Caches->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::Caches->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::Caches->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 d880b2c..d238bdf 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -179,6 +179,33 @@ sub current { return $context; } +=head2 cache + +Returns the cache object or undef + +=cut + +sub cache { + my $self = shift; + $self = $context unless ref ($self); + + return Koha::Caches->get_instance($self->{namespace}); +} + +=head2 memcached + +Returns the cache object if it is memcached or undef + +=cut + +sub memcached { + my $self = shift; + my $cache = $self->cache; + return $cache if $cache->memcached_cache; + + return; +} + sub db_driver { my $self = shift; @@ -497,8 +524,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 diff --git a/C4/External/OverDrive.pm b/C4/External/OverDrive.pm index 9044169..0e71707 100644 --- a/C4/External/OverDrive.pm +++ b/C4/External/OverDrive.pm @@ -22,7 +22,7 @@ use warnings; use Koha; use JSON; -use Koha::Caches; +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::Caches->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 036b4f7..a84437a 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -443,7 +443,7 @@ Returns item record sub _build_default_values_for_mod_marc { my ($frameworkcode) = @_; - my $cache = Koha::Caches->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 f547fe0..05a229d 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -24,7 +24,6 @@ use strict; #use warnings; FIXME - Bug 2505 use C4::Context; -use Koha::Caches; use Koha::DateUtils qw(dt_from_string); use Koha::AuthorisedValues; use Koha::Libraries; @@ -533,7 +532,7 @@ sub GetAuthorisedValues { C4::Context->userenv ? C4::Context->userenv->{"branch"} : ""; my $cache_key = "AuthorisedValues-$category-$opac-$branch_limit"; - my $cache = Koha::Caches->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 8713489..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::Caches; sub get_yaml { my $yml_path = C4::Context->config('intranetdir') . '/admin/columns_settings.yml'; - my $cache = Koha::Caches->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 5af8c0e..26dd8f0 100644 --- a/Koha/Cache.pm +++ b/Koha/Cache.pm @@ -64,34 +64,35 @@ Create a new Koha::Cache object. This is required for all cache-related function =cut sub new { - my ( $class, $self, $params ) = @_; + my ( $class, $self ) = @_; $self->{'default_type'} = $self->{cache_type} || $ENV{CACHING_SYSTEM} # DELME What about this? || 'memcached'; - my $subnamespace = $params->{subnamespace} // ''; + $self->{namespace} //= ''; $ENV{DEBUG} && carp "Default caching system: $self->{'default_type'}"; $self->{'timeout'} ||= 0; - # Should we continue to support MEMCACHED ENV vars? - $self->{'namespace'} ||= $ENV{MEMCACHED_NAMESPACE}; - my @servers = split /,/, $ENV{MEMCACHED_SERVERS} || ''; - unless ( $self->{namespace} and @servers ) { - my $koha_config = Koha::Config->read_from_file( Koha::Config->guess_koha_conf() ); - $self->{namespace} ||= $koha_config->{config}{memcached_namespace} || 'koha'; - @servers = split /,/, $koha_config->{config}{memcached_servers} // '' - unless @servers; - } - $self->{namespace} .= ":$subnamespace:"; if ( $self->{'default_type'} eq 'memcached' - && can_load( modules => { 'Cache::Memcached::Fast' => undef } ) - && _initialize_memcached($self, @servers) - && defined( $self->{'memcached_cache'} ) ) + && can_load( modules => { 'Cache::Memcached::Fast' => undef } ) ) { - $self->{'cache'} = $self->{'memcached_cache'}; + # Should we continue to support MEMCACHED ENV vars? + $self->{'namespace'} ||= $ENV{MEMCACHED_NAMESPACE}; + my @servers = split /,/, $ENV{MEMCACHED_SERVERS} || ''; + unless ( $self->{namespace} && @servers ) { + my $koha_config = Koha::Config->read_from_file( Koha::Config->guess_koha_conf() ); + $self->{namespace} ||= $koha_config->{config}{memcached_namespace} || 'koha'; + @servers = split /,/, $koha_config->{config}{memcached_servers} // '' + unless @servers; + } + if ( _initialize_memcached($self, @servers) + && defined( $self->{'memcached_cache'} ) ) + { + $self->{'cache'} = $self->{'memcached_cache'}; + } } if ( $self->{'default_type'} eq 'fastmmap' diff --git a/Koha/Caches.pm b/Koha/Caches.pm index 51ab816..190e315 100644 --- a/Koha/Caches.pm +++ b/Koha/Caches.pm @@ -58,7 +58,7 @@ sub get_instance { =cut sub flush_L1_caches { - my ($class, $subnamespace, $namespace) = @_; + my ($class, $namespace) = @_; $namespace //= ''; return unless $singleton_caches; diff --git a/Koha/Calendar.pm b/Koha/Calendar.pm index 67d2a6b..634a5ad 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::Caches; use Carp; sub new { @@ -55,7 +54,7 @@ sub _init { sub exception_holidays { my ( $self ) = @_; - my $cache = Koha::Caches->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::Caches->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..3760679 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_UNSAFE_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_UNSAFE_PROPERTIES) { + # Clean slate + $cache->clear_from_cache($_); + } + } + 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_UNSAFE_PROPERTIES) { + if (my $shared = $cache->get($_)) { + $context->{$_} = $shared; + } + } + } + + my $ret = $context->run_within_context(sub { $app->($env) }); + + if ($shared_context && $cache) { + foreach (@CONTEXT_UNSAFE_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/MetaSearcher.pm b/Koha/MetaSearcher.pm index 65f7239..73c78ae 100644 --- a/Koha/MetaSearcher.pm +++ b/Koha/MetaSearcher.pm @@ -25,7 +25,7 @@ use C4::Charset qw( MarcToUTF8Record ); use C4::Search qw(); # Purely for new_record_from_zebra use DBIx::Class::ResultClass::HashRefInflator; use IO::Select; -use Koha::Caches; +use C4::Context; use Koha::Database; use Koha::MetadataRecord; use MARC::File::XML; @@ -91,7 +91,7 @@ sub search { my $resultset_expiry = 300; - my $cache = Koha::Caches->get_instance(); + my $cache = C4::Context->cache; my $schema = Koha::Database->new->schema; my $stats = { num_fetched => { diff --git a/Koha/Template/Plugin/Cache.pm b/Koha/Template/Plugin/Cache.pm index 5e3f8f7..4c88b80 100644 --- a/Koha/Template/Plugin/Cache.pm +++ b/Koha/Template/Plugin/Cache.pm @@ -21,7 +21,6 @@ use strict; use warnings; use base qw( Template::Plugin ); use Template::Plugin; -use C4::Context; #------------------------------------------------------------------------ # new(\%options) @@ -34,8 +33,7 @@ sub new { $cache = delete $params->{cache}; } else { - require Koha::Cache; - $cache = Koha::Caches->get_instance(); + $cache = $context->cache; } my $self = bless { CACHE => $cache, diff --git a/about.pl b/about.pl index e41759f..645884f 100755 --- a/about.pl +++ b/about.pl @@ -36,7 +36,6 @@ use C4::Installer; use Koha; use Koha::Acquisition::Currencies; use Koha::Patrons; -use Koha::Caches; use Koha::Config::SysPrefs; use C4::Members::Statistics; @@ -75,7 +74,7 @@ if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) { my $memcached_servers = $ENV{MEMCACHED_SERVERS} || C4::Context->config('memcached_servers'); my $memcached_namespace = $ENV{MEMCACHED_NAMESPACE} || C4::Context->config('memcached_namespace') // 'koha'; -my $cache = Koha::Caches->get_instance; +my $cache = C4::Context->cache; my $effective_caching_method = ref($cache->cache); # Memcached may have been running when plack has been initialized but could have been stopped since # FIXME What are the consequences of that?? diff --git a/admin/biblio_framework.pl b/admin/biblio_framework.pl index dd4af97..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::Caches; my $input = new CGI; my $frameworkcode = $input->param('frameworkcode') || q||; my $op = $input->param('op') || q|list|; -my $cache = Koha::Caches->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 d8fd569..68f1e55 100755 --- a/admin/koha2marclinks.pl +++ b/admin/koha2marclinks.pl @@ -59,7 +59,7 @@ else { } my $dbh = C4::Context->dbh; -my $cache = Koha::Caches->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 077a58b..514fa1f 100755 --- a/admin/marc_subfields_structure.pl +++ b/admin/marc_subfields_structure.pl @@ -79,7 +79,7 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user( debug => 1, } ); -my $cache = Koha::Caches->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 4800b34..52c3c6f 100755 --- a/admin/marctagstructure.pl +++ b/admin/marctagstructure.pl @@ -25,11 +25,6 @@ use C4::Auth; use C4::Koha; use C4::Context; use C4::Output; -use C4::Context; - -use Koha::Caches; -use Koha::AuthorisedValues; -use Koha::BiblioFrameworks; # retrieve parameters my $input = new CGI; @@ -46,7 +41,7 @@ my $pagesize = 20; my $script_name = "/cgi-bin/koha/admin/marctagstructure.pl"; my $dbh = C4::Context->dbh; -my $cache = Koha::Caches->get_instance(); +my $cache = C4::Context->cache; # open template my ($template, $loggedinuser, $cookie) diff --git a/opac/svc/report b/opac/svc/report index f83b12e..3d44027 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::Caches; - 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::Caches->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/reports/guided_reports.pl b/reports/guided_reports.pl index bfd0a76..0779942 100755 --- a/reports/guided_reports.pl +++ b/reports/guided_reports.pl @@ -29,7 +29,6 @@ use C4::Auth qw/:DEFAULT get_session/; use C4::Output; use C4::Debug; use C4::Context; -use Koha::Caches; use C4::Log; use Koha::DateUtils qw/dt_from_string output_pref/; use Koha::AuthorisedValue; @@ -49,7 +48,7 @@ Script to control the guided report creation =cut my $input = new CGI; -my $usecache = Koha::Caches->get_instance->memcached_cache; +my $usecache = C4::Context->cache->memcached_cache; my $phase = $input->param('phase') // ''; my $flagsrequired; diff --git a/svc/report b/svc/report index 0188eaa..4e872c3 100755 --- a/svc/report +++ b/svc/report @@ -25,7 +25,7 @@ use C4::Reports::Guided; use JSON; use CGI qw ( -utf8 ); -use Koha::Caches; +use C4::Context; my $query = CGI->new(); @@ -48,7 +48,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( } ); -my $cache = Koha::Caches->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/Calendar.t b/t/Calendar.t index d5b8967..3cf4074 100755 --- a/t/Calendar.t +++ b/t/Calendar.t @@ -22,7 +22,7 @@ use Test::MockModule; use DateTime; use DateTime::Duration; -use Koha::Caches; +use C4::Context; use Koha::DateUtils; use Module::Load::Conditional qw/check_install/; @@ -84,7 +84,7 @@ fixtures_ok [ ], ], "add fixtures"; -my $cache = Koha::Caches->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 36ef4d7..627eab2 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 64759fa..bd1347b 100644 --- a/t/db_dependent/Filter_MARC_ViewPolicy.t +++ b/t/db_dependent/Filter_MARC_ViewPolicy.t @@ -30,7 +30,6 @@ use MARC::Record; use MARC::Field; use C4::Context; use C4::Biblio; -use Koha::Caches; use Koha::Database; BEGIN { @@ -66,7 +65,7 @@ sub run_hiding_tests { $sth->execute($hidden_value); - my $cache = Koha::Caches->get_instance(); + my $cache = C4::Context->cache; $cache->flush_all(); # easy way to ensure DB is queried again. my $processor = Koha::RecordProcessor->new( @@ -138,7 +137,7 @@ sub run_hiding_tests { $sth->execute(-1); # -1 is visible in opac and intranet. - my $cache = Koha::Caches->get_instance(); + my $cache = C4::Context->cache; $cache->flush_all(); # easy way to ensure DB is queried again. my $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc( diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t index f85d48a..ff94561 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; @@ -410,7 +411,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::Caches->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"); @@ -710,7 +711,7 @@ subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { |, undef, $framework->{frameworkcode} ); # And make sure the caches are cleared - my $cache = Koha::Caches->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 eb948c7..5a9eeda 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::Caches; +use C4::Context; use C4::Calendar; use DateTime; @@ -128,6 +128,6 @@ sub add_holiday { } } # we updated the single_holidays table, so wipe its cache - my $cache = Koha::Caches->get_instance(); + my $cache = C4::Context->cache; $cache->clear_from_cache( 'single_holidays') ; } -- 2.7.4