Bug 23137: Move cache flushing to the method

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Jonathan Druart 2020-04-15 16:38:16 +02:00 committed by Martin Renvoize
parent 8cd362a422
commit 11bf5d7afa
Signed by: martin.renvoize
GPG key ID: 422B469130441A0F
4 changed files with 22 additions and 13 deletions

View file

@ -26,6 +26,7 @@ use Koha::Exceptions::Config;
use Koha::Exceptions::Elasticsearch; use Koha::Exceptions::Elasticsearch;
use Koha::SearchFields; use Koha::SearchFields;
use Koha::SearchMarcMaps; use Koha::SearchMarcMaps;
use Koha::Caches;
use C4::Heading; use C4::Heading;
use Carp; use Carp;
@ -382,6 +383,12 @@ sub reset_elasticsearch_mappings {
} }
} }
} }
my $cache = Koha::Caches->get_instance();
$cache->clear_from_cache('elasticsearch_search_fields_staff_client');
$cache->clear_from_cache('elasticsearch_search_fields_opac');
# FIXME return the mappings?
} }
# This overrides the accessor provided by Class::Accessor so that if # This overrides the accessor provided by Class::Accessor so that if

View file

@ -78,12 +78,6 @@ my $update_mappings = sub {
} }
}; };
my $cache = Koha::Caches->get_instance();
my $clear_cache = sub {
$cache->clear_from_cache('elasticsearch_search_fields_staff_client');
$cache->clear_from_cache('elasticsearch_search_fields_opac');
};
if ( $op eq 'edit' ) { if ( $op eq 'edit' ) {
$schema->storage->txn_begin; $schema->storage->txn_begin;
@ -170,13 +164,16 @@ if ( $op eq 'edit' ) {
} else { } else {
push @messages, { type => 'message', code => 'success_on_update' }; push @messages, { type => 'message', code => 'success_on_update' };
$schema->storage->txn_commit; $schema->storage->txn_commit;
$clear_cache->();
my $cache = Koha::Caches->get_instance();
$cache->clear_from_cache('elasticsearch_search_fields_staff_client');
$cache->clear_from_cache('elasticsearch_search_fields_opac');
$update_mappings->(); $update_mappings->();
} }
} }
elsif( $op eq 'reset_confirmed' ) { elsif( $op eq 'reset_confirmed' ) {
Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings; Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
$clear_cache->();
push @messages, { type => 'message', code => 'success_on_reset' }; push @messages, { type => 'message', code => 'success_on_reset' };
} }
elsif( $op eq 'reset_confirm' ) { elsif( $op eq 'reset_confirm' ) {

View file

@ -112,7 +112,6 @@ use Koha::MetadataRecord::Authority;
use Koha::BiblioUtils; use Koha::BiblioUtils;
use Koha::SearchEngine::Elasticsearch; use Koha::SearchEngine::Elasticsearch;
use Koha::SearchEngine::Elasticsearch::Indexer; use Koha::SearchEngine::Elasticsearch::Indexer;
use Koha::Caches;
use MARC::Field; use MARC::Field;
use MARC::Record; use MARC::Record;
use Modern::Perl; use Modern::Perl;
@ -156,9 +155,6 @@ _sanity_check();
if ($reset){ if ($reset){
Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings; Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
my $cache = Koha::Caches->get_instance();
$cache->clear_from_cache('elasticsearch_search_fields_staff_client');
$cache->clear_from_cache('elasticsearch_search_fields_opac');
$delete = 1; $delete = 1;
} }

View file

@ -17,10 +17,11 @@
use Modern::Perl; use Modern::Perl;
use Test::More tests => 4; use Test::More tests => 5;
use Test::MockModule; use Test::MockModule;
use Koha::Database; use Koha::Database;
use Koha::Caches;
my $indexes = { my $indexes = {
'authorities' => { 'authorities' => {
@ -54,8 +55,12 @@ Koha::SearchFields->search->delete;
Koha::SearchMarcMaps->search->delete; Koha::SearchMarcMaps->search->delete;
$schema->resultset('SearchMarcToField')->search->delete; $schema->resultset('SearchMarcToField')->search->delete;
Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings; Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
my $cache = Koha::Caches->get_instance();
is( $cache->get_from_cache('elasticsearch_search_fields_staff_client'), undef, 'Cache has been flushed by reset_elasticsearch_mappings' );
my $search_fields = Koha::SearchFields->search({}); my $search_fields = Koha::SearchFields->search({});
is($search_fields->count, 2, 'There is 2 search fields after reset'); is($search_fields->count, 2, 'There is 2 search fields after reset');
@ -66,3 +71,7 @@ my $title_sf = Koha::SearchFields->search({ name => 'title' })->next;
is($title_sf->weight, '20.00', 'Title search field is weighted with 20'); is($title_sf->weight, '20.00', 'Title search field is weighted with 20');
$schema->storage->txn_rollback; $schema->storage->txn_rollback;
$cache = Koha::Caches->get_instance();
$cache->clear_from_cache('elasticsearch_search_fields_staff_client');
$cache->clear_from_cache('elasticsearch_search_fields_opac');