la-haute-societe/craft-elasticsearch

isIndexInSync will always result in a cache miss if index is not in sync

Closed this issue · 1 comments

aloco commented

Hi,

I have an installation maintaining around 60K entries.

I noticed slow response times in CP and figured out that

return $application->cache->getOrSet(
self::getSyncCachekey(),
function (): bool {
Craft::debug('isIndexInSync cache miss', __METHOD__);
if ($this->testConnection() === false) {
return false;
}
$sites = Craft::$app->getSites();
$outOfSync = false;
foreach ($sites->getAllSites() as $site) {
$sites->setCurrentSite($site);
ElasticsearchRecord::$siteId = $site->id;
Craft::debug('Checking Elasticsearch index for site #' . $site->id, __METHOD__);
$countIdexableElements = $this->countIndexableEntries($site->id);
$countEsRecords = (int)ElasticsearchRecord::find()->count();
Craft::debug(
sprintf(
'Site %s: %d indexable elements, %d in Elasticsearch index.',
$site->handle,
$countIdexableElements,
$countEsRecords
),
__METHOD__
);
if ($countIdexableElements !== $countEsRecords) {
$outOfSync = true;
}
}
if ($outOfSync === true) {
Craft::debug('Elasticsearch reindex needed!', __METHOD__);
return false;
}
return true;
},
300

causes SELECT COUNT(*) queries on every CP request which are slow. (>1 second with 60K entries)

Since you are using a boolean as cached value, getOrSet will always lead to a cache miss, as soon as false is getting cached which will trigger slow queries on every CP request making working with the dashboard very frustrating. You could use a string instead of a boolean to mitigate that problem.

Closed by 8f181cd.
Version 2.1.0 is out with that change.

Thanks you for your help on this!