diff --git a/services/search.py b/services/search.py index 3384a6ab..f599a1d1 100644 --- a/services/search.py +++ b/services/search.py @@ -1,5 +1,6 @@ import asyncio - +import json +from base.redis import redis from orm.shout import Shout from resolvers.zine import load_shouts_by @@ -16,9 +17,15 @@ class SearchService: @staticmethod async def search(text, limit, offset) -> [Shout]: - async with SearchService.lock: - by = { - "title": text, - "body": text - } - return await load_shouts_by(None, None, by, limit, offset) + cached = redis.execute("GET", text) + if not cached: + async with SearchService.lock: + by = { + "title": text, + "body": text + } + payload = await load_shouts_by(None, None, by, limit, offset) + redis.execute("SET", text, json.dumps(payload)) + return payload + else: + return json.loads(cached)