perf: cache résultats de recherche par requête, invalidé sur create/update/delete

This commit is contained in:
Cedric Abonnel
2026-05-12 23:34:51 +02:00
parent 25faa6ac4f
commit fb14d7c842
6 changed files with 97 additions and 26 deletions
+47
View File
@@ -649,6 +649,53 @@ class ArticleManager
$this->dataDir . '/search_index.json',
json_encode($index, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
);
$this->clearSearchResultsCache();
}
/** Vide le cache des résultats de recherche (appelé après chaque modification). */
public function clearSearchResultsCache(): void
{
$dir = $this->dataDir . '/_cache/search';
if (!is_dir($dir)) {
return;
}
foreach (scandir($dir) as $f) {
if (str_ends_with($f, '.json')) {
@unlink($dir . '/' . $f);
}
}
}
/**
* Retourne les résultats mis en cache pour une requête anonyme, ou null si absent.
* @return array<array>|null
*/
public function getSearchCache(string $query): ?array
{
$path = $this->searchCachePath($query);
if (!file_exists($path)) {
return null;
}
$data = json_decode((string) file_get_contents($path), true);
return is_array($data) ? $data : null;
}
/** Persiste les résultats d'une requête anonyme dans le cache. */
public function setSearchCache(string $query, array $results): void
{
$dir = $this->dataDir . '/_cache/search';
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
file_put_contents(
$this->searchCachePath($query),
json_encode($results, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
);
}
private function searchCachePath(string $query): string
{
return $this->dataDir . '/_cache/search/' . md5(mb_strtolower(trim($query))) . '.json';
}
/** Retourne l'index pré-construit, ou null s'il n'existe pas encore. */