core/services/search.py

32 lines
874 B
Python
Raw Normal View History

2022-10-04 00:32:29 +00:00
import asyncio
2022-11-17 19:53:58 +00:00
import json
from base.redis import redis
2022-10-04 00:32:29 +00:00
from orm.shout import Shout
from resolvers.zine import load_shouts_by
2022-10-04 00:32:29 +00:00
class SearchService:
lock = asyncio.Lock()
cache = {}
@staticmethod
2022-10-04 09:25:59 +00:00
async def init(session):
async with SearchService.lock:
2022-11-15 12:04:22 +00:00
print('[search.service] init')
2022-10-04 09:25:59 +00:00
SearchService.cache = {}
2022-10-04 00:32:29 +00:00
@staticmethod
async def search(text, limit, offset) -> [Shout]:
2022-11-17 19:53:58 +00:00
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)