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
|
2022-11-21 08:13:57 +00:00
|
|
|
from resolvers.zine.load 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-21 22:23:16 +00:00
|
|
|
print('[search.service] did nothing')
|
2022-10-04 09:25:59 +00:00
|
|
|
SearchService.cache = {}
|
2022-10-04 00:32:29 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-11-17 19:47:40 +00:00
|
|
|
async def search(text, limit, offset) -> [Shout]:
|
2022-11-17 20:29:04 +00:00
|
|
|
cached = await redis.execute("GET", text)
|
2022-11-17 19:53:58 +00:00
|
|
|
if not cached:
|
|
|
|
async with SearchService.lock:
|
|
|
|
by = {
|
|
|
|
"title": text,
|
|
|
|
"body": text
|
|
|
|
}
|
|
|
|
payload = await load_shouts_by(None, None, by, limit, offset)
|
2022-11-17 20:29:04 +00:00
|
|
|
await redis.execute("SET", text, json.dumps(payload))
|
2022-11-17 19:53:58 +00:00
|
|
|
return payload
|
|
|
|
else:
|
|
|
|
return json.loads(cached)
|