redis cached

This commit is contained in:
tonyrewin 2022-11-17 22:53:58 +03:00
parent f764577136
commit 9cfad11df2

View File

@ -1,5 +1,6 @@
import asyncio import asyncio
import json
from base.redis import redis
from orm.shout import Shout from orm.shout import Shout
from resolvers.zine import load_shouts_by from resolvers.zine import load_shouts_by
@ -16,9 +17,15 @@ class SearchService:
@staticmethod @staticmethod
async def search(text, limit, offset) -> [Shout]: async def search(text, limit, offset) -> [Shout]:
async with SearchService.lock: cached = redis.execute("GET", text)
by = { if not cached:
"title": text, async with SearchService.lock:
"body": text by = {
} "title": text,
return await load_shouts_by(None, None, by, limit, offset) "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)