From b502c581f7d4973070ee300e877625be8962ddb1 Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 19 Dec 2023 15:42:46 +0300 Subject: [PATCH] search-result-schema-fix-5 --- services/search.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/services/search.py b/services/search.py index a9166aa1..8844b13d 100644 --- a/services/search.py +++ b/services/search.py @@ -19,10 +19,11 @@ class SearchService: @staticmethod async def search(text: str, limit: int = 50, offset: int = 0) -> List[Shout]: + payload = [] try: - payload = await redis.execute("GET", text) + cached = await redis.execute("GET", text) - if not payload: + if not cached: async with SearchService.lock: # Use aiohttp to send a request to ElasticSearch async with aiohttp.ClientSession() as session: @@ -33,8 +34,9 @@ class SearchService: await redis.execute("SET", text, json.dumps(payload)) # use redis as cache else: logging.error(f"[services.search] response: {response.status} {await response.text()}") + else: + payload = json.loads(cached) except Exception as e: logging.error(f"[services.search] Error during search: {e}") - payload = [] return payload[offset : offset + limit]