perf: cache résultats de recherche par requête, invalidé sur create/update/delete
This commit is contained in:
@@ -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. */
|
||||
|
||||
Reference in New Issue
Block a user