Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpmlbridge-300] Cache cluster indices. #51

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/Manager/Indices.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class Indices {
/** @var string|null */
private $stopwordFilterSlug = null;

/** @var string[]|null */
private static $clusterIndices = null;

/**
* @param Elasticsearch $elasticsearch
* @param Indexables $indexables
Expand Down Expand Up @@ -90,13 +93,46 @@ public function clearCurrentIndexLanguage() {
$this->currentIndexLanguage = '';
}

/**
* @return string[]
*/
private function getClusterIndicesCache() {
if ( null === self::$clusterIndices ) {
$clusterIndices = $this->elasticsearch->get_cluster_indices();
self::$clusterIndices = wp_list_pluck( $clusterIndices, 'index' );
}

return self::$clusterIndices;
}

/**
* @param string $indexName
*/
private function saveIndexInClusterCache( $indexName ) {
if ( null === self::$clusterIndices ) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self::getClusterIndicesCache() - shouldn't it "load cache" and add it there? I ask out of curiosity

self::$clusterIndices = [];
}

if ( in_array( $indexName, self::$clusterIndices, true ) ) {
return;
}

self::$clusterIndices[] = $indexName;
}

private function clearClusterIndicesCache() {
self::$clusterIndices = null;
}

/**
* @param string $indexName
*
* @return bool
*/
public function indexExists( $indexName ) {
return $this->elasticsearch->index_exists( $indexName );
$clusterIndices = $this->getClusterIndicesCache();

return in_array( $indexName, $clusterIndices, true );
}

/**
Expand Down Expand Up @@ -155,6 +191,7 @@ public function generateIndexByIndexable( $indexable ) {
];

$this->elasticsearch->put_mapping( $indexName, $mapping );
$this->saveIndexInClusterCache( $indexName );
}

/**
Expand All @@ -168,6 +205,7 @@ public function generateIndexableIndexes( $indexables ) {

public function clearAllIndices() {
$this->elasticsearch->delete_all_indices();
$this->clearClusterIndicesCache();
}

public function generateMissingIndices() {
Expand Down
10 changes: 10 additions & 0 deletions tests/phpstan/stubs/elasticpress.stub
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ namespace ElasticPress {
*/
public function put_mapping( $index, $mapping, $return_type = 'bool' ) {}

/**
* @return string[]
*/
public function get_cluster_indices() {}

/**
* @return boolean
*/
Expand Down Expand Up @@ -160,6 +165,11 @@ namespace ElasticPress {
*/
public function get_index_name( $blog_id = null ) {}

/**
* @return array
*/
public function get_indexable_post_status() {}

/**
* @return array
*/
Expand Down
Loading