From 73a6cdcf3cfc383d419dac0dd22cb30048cfe9d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nu=C5=A1a=20Puk=C5=A1i=C4=8D?= Date: Fri, 10 Oct 2025 15:12:37 +0200 Subject: [PATCH] Faster search --- src/Twig/Components/SearchComponent.php | 14 +++++++++++++- templates/components/SearchComponent.html.twig | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Twig/Components/SearchComponent.php b/src/Twig/Components/SearchComponent.php index ccef292..d8c5bba 100644 --- a/src/Twig/Components/SearchComponent.php +++ b/src/Twig/Components/SearchComponent.php @@ -3,6 +3,7 @@ namespace App\Twig\Components; use App\Credits\Service\CreditsManager; +use App\Service\RedisCacheService; use FOS\ElasticaBundle\Finder\FinderInterface; use Psr\Cache\InvalidArgumentException; use Psr\Log\LoggerInterface; @@ -28,6 +29,7 @@ final class SearchComponent #[LiveProp(writable: true, useSerializerForHydration: true)] public string $query = ''; public array $results = []; + public array $authors = []; public bool $interactive = true; public string $currentRoute; @@ -57,7 +59,8 @@ final class SearchComponent private readonly TokenStorageInterface $tokenStorage, private readonly LoggerInterface $logger, private readonly CacheInterface $cache, - private readonly RequestStack $requestStack + private readonly RequestStack $requestStack, + private readonly RedisCacheService $redisCacheService ) { } @@ -84,6 +87,8 @@ final class SearchComponent if ($session->has(self::SESSION_QUERY_KEY)) { $this->query = $session->get(self::SESSION_QUERY_KEY); $this->results = $session->get(self::SESSION_KEY, []); + $pubkeys = array_unique(array_map(fn($art) => $art->getPubkey(), $this->results)); + $this->authors = $this->redisCacheService->getMultipleMetadata($pubkeys); $this->logger->info('Restored search results from session for query: ' . $this->query); } } @@ -102,6 +107,7 @@ final class SearchComponent if (empty($this->query)) { $this->results = []; + $this->authors = []; $this->clearSearchCache(); return; } @@ -117,12 +123,15 @@ final class SearchComponent if ($session->has(self::SESSION_QUERY_KEY) && $session->get(self::SESSION_QUERY_KEY) === $this->query) { $this->results = $session->get(self::SESSION_KEY, []); + $pubkeys = array_unique(array_map(fn($art) => $art->getPubkey(), $this->results)); + $this->authors = $this->redisCacheService->getMultipleMetadata($pubkeys); $this->logger->info('Using cached search results for query: ' . $this->query); return; } if (!$this->creditsManager->canAfford($this->npub, 1)) { $this->results = []; + $this->authors = []; return; } @@ -133,6 +142,8 @@ final class SearchComponent // Perform optimized single search query $this->results = $this->performOptimizedSearch($this->query); + $pubkeys = array_unique(array_map(fn($art) => $art->getPubkey(), $this->results)); + $this->authors = $this->redisCacheService->getMultipleMetadata($pubkeys); // Cache the search results in session $this->saveSearchToSession($this->query, $this->results); @@ -140,6 +151,7 @@ final class SearchComponent } catch (\Exception $e) { $this->logger->error('Search error: ' . $e->getMessage()); $this->results = []; + $this->authors = []; } } diff --git a/templates/components/SearchComponent.html.twig b/templates/components/SearchComponent.html.twig index f2a4805..c694e9d 100644 --- a/templates/components/SearchComponent.html.twig +++ b/templates/components/SearchComponent.html.twig @@ -58,7 +58,7 @@

{{ art.title }}