@@ -51,6 +51,9 @@ class ReplicaManager implements ReplicaManagerInterface
51
51
/** @var array<int, string[]> */
52
52
protected array $ _magentoReplicaPossibleConfig = [];
53
53
54
+ /** @var array<int, string[]> */
55
+ protected array $ _unusedReplicaIndices = [];
56
+
54
57
public function __construct (
55
58
protected ConfigHelper $ configHelper ,
56
59
protected AlgoliaHelper $ algoliaHelper ,
@@ -253,7 +256,7 @@ protected function setReplicasOnPrimaryIndex(int $storeId): array
253
256
$ setReplicasTaskId = $ this ->algoliaHelper ->getLastTaskId ();
254
257
$ this ->algoliaHelper ->waitLastTask ($ indexName , $ setReplicasTaskId );
255
258
$ this ->clearAlgoliaReplicaSettingCache ($ indexName );
256
- $ this ->deleteReplicaIndices ($ replicasToDelete );
259
+ $ this ->deleteIndices ($ replicasToDelete );
257
260
258
261
if (self ::_DEBUG ) {
259
262
$ this ->logger ->log (
@@ -345,7 +348,7 @@ protected function getBareIndexNameFromReplicaSetting(string $replicaSetting): s
345
348
* @return void
346
349
* @throws AlgoliaException
347
350
*/
348
- protected function deleteReplicaIndices (array $ replicasToDelete ): void
351
+ protected function deleteIndices (array $ replicasToDelete ): void
349
352
{
350
353
foreach ($ replicasToDelete as $ deletedReplica ) {
351
354
$ this ->algoliaHelper ->deleteIndex ($ deletedReplica );
@@ -426,16 +429,15 @@ protected function clearReplicasSettingInAlgolia(string $primaryIndexName): void
426
429
*/
427
430
public function deleteReplicasFromAlgolia (int $ storeId , bool $ unused = false ): void
428
431
{
429
- $ primaryIndexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
430
-
431
432
if ($ unused ) {
432
- $ replicasToDelete = $ this ->getUnusedReplicaIndices ($ primaryIndexName );
433
+ $ replicasToDelete = $ this ->getUnusedReplicaIndices ($ storeId );
433
434
} else {
435
+ $ primaryIndexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
434
436
$ replicasToDelete = $ this ->getMagentoReplicaIndicesFromAlgolia ($ primaryIndexName );
435
437
$ this ->clearReplicasSettingInAlgolia ($ primaryIndexName );
436
438
}
437
439
438
- $ this ->deleteReplicaIndices ($ replicasToDelete );
440
+ $ this ->deleteIndices ($ replicasToDelete );
439
441
}
440
442
441
443
/**
@@ -447,29 +449,31 @@ protected function getMagentoReplicaIndicesFromAlgolia(string $primaryIndexName)
447
449
}
448
450
449
451
/**
450
- * @return string[]
451
- * @throws NoSuchEntityException
452
- * @throws LocalizedException
453
- * @throws AlgoliaException
452
+ * @inheritDoc
454
453
*/
455
- protected function getUnusedReplicaIndices (string $ primaryIndexName ): array
454
+ public function getUnusedReplicaIndices (int $ storeId ): array
456
455
{
457
- $ currentReplicas = $ this ->getMagentoReplicaIndicesFromAlgolia ($ primaryIndexName );
458
- $ unusedReplicas = [];
459
- $ allIndices = $ this ->algoliaHelper ->listIndexes ();
460
-
461
- foreach ($ allIndices ['items ' ] as $ indexInfo ) {
462
- $ indexName = $ indexInfo ['name ' ];
463
- if ($ this ->isMagentoReplicaIndex ($ indexName , $ primaryIndexName )
464
- && !$ this ->indexNameFetcher ->isTempIndex ($ indexName )
465
- && !$ this ->indexNameFetcher ->isQuerySuggestionsIndex ($ indexName )
466
- && !in_array ($ indexName , $ currentReplicas ))
467
- {
468
- $ unusedReplicas [] = $ indexName ;
456
+ $ primaryIndexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
457
+ if (!isset ($ this ->_unusedReplicaIndices [$ storeId ])) {
458
+ $ currentReplicas = $ this ->getMagentoReplicaIndicesFromAlgolia ($ primaryIndexName );
459
+ $ unusedReplicas = [];
460
+ $ allIndices = $ this ->algoliaHelper ->listIndexes ();
461
+
462
+ foreach ($ allIndices ['items ' ] as $ indexInfo ) {
463
+ $ indexName = $ indexInfo ['name ' ];
464
+ if ($ this ->isMagentoReplicaIndex ($ indexName , $ primaryIndexName )
465
+ && !$ this ->indexNameFetcher ->isTempIndex ($ indexName )
466
+ && !$ this ->indexNameFetcher ->isQuerySuggestionsIndex ($ indexName )
467
+ && !in_array ($ indexName , $ currentReplicas ))
468
+ {
469
+ $ unusedReplicas [] = $ indexName ;
470
+ }
469
471
}
472
+ $ this ->_unusedReplicaIndices [$ storeId ] = $ unusedReplicas ;
470
473
}
471
474
472
- return $ unusedReplicas ;
475
+
476
+ return $ this ->_unusedReplicaIndices [$ storeId ];
473
477
}
474
478
475
479
/**
0 commit comments