Skip to content

Commit c8a97f8

Browse files
author
Mustafa Şükrü Kapusuz
committed
refactor: perf improvement (prevent twice settings pulling)
1 parent 24f6bdf commit c8a97f8

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

includes/indices/class-algolia-index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* @since 1.0.0
1919
*/
2020
abstract class Algolia_Index {
21+
const AC_INDEX_NOT_EXISTS_EXCEPTION_MSG = 'Index does not exist';
22+
2123
/**
2224
* Default index settings
2325
*

includes/indices/settings/class-algolia-primary-index-settings.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use WebDevStudios\WPSWA\Algolia\AlgoliaSearch\SearchIndex;
4+
use WebDevStudios\WPSWA\Algolia\AlgoliaSearch\Exceptions\NotFoundException;
45

56
class Algolia_Primary_Index_Settings implements Algolia_Index_Settings {
67
protected Algolia_Index $index;
@@ -27,12 +28,23 @@ public function get_local_settings(): array {
2728
return $this->get_index()->get_default_settings();
2829
}
2930

31+
/**
32+
* If the Indice is not found, return empty settings. In that case, a new index is created.
33+
*
34+
* @throws Exception Varius exceptions can be thrown by Algolia Client except for NotFoundException.
35+
* @return array
36+
*/
3037
public function get_remote_settings(): array {
31-
if ( ! $this->get_index()->exists() ) {
32-
return [];
33-
}
38+
try {
39+
return $this->get_algolia_index()->getSettings();
40+
} catch ( NotFoundException $e ) {
41+
// being more strict, catch only index does not exists case.
42+
if ( Algolia_Index::AC_INDEX_NOT_EXISTS_EXCEPTION_MSG === $e->getMessage() ) {
43+
return [];
44+
}
3445

35-
return $this->get_algolia_index()->getSettings();
46+
throw $e;
47+
}
3648
}
3749

3850
public function get_settings_needs_sync(): array {
@@ -63,7 +75,7 @@ public function push( $overrides = [] ): bool {
6375
}
6476

6577
try {
66-
$this->get_algolia_index()->setSettings( $settings );
78+
$this->get_algolia_index()->setSettings( $settings ); // Creates new indice if doesn't exist.
6779
} catch ( Exception $e ) {
6880
return false;
6981
}

0 commit comments

Comments
 (0)