Files
core/services/search.py

35 lines
1016 B
Python
Raw Normal View History

2022-10-04 03:32:29 +03:00
import asyncio
2022-11-17 22:53:58 +03:00
import json
2023-10-23 17:47:11 +03:00
from services.rediscache import redis
2023-10-05 23:31:21 +03:00
from orm.shout import Shout
2023-11-22 21:23:15 +03:00
from resolvers.reader import load_shouts_by
2022-10-04 03:32:29 +03:00
class SearchService:
lock = asyncio.Lock()
cache = {}
@staticmethod
2022-10-04 12:25:59 +03:00
async def init(session):
async with SearchService.lock:
2023-10-06 01:12:34 +03:00
print("[search] did nothing")
2022-10-04 12:25:59 +03:00
SearchService.cache = {}
2022-10-04 03:32:29 +03:00
@staticmethod
async def search(text, limit, offset) -> [Shout]:
2022-11-17 23:29:04 +03:00
cached = await redis.execute("GET", text)
2022-11-17 22:53:58 +03:00
if not cached:
async with SearchService.lock:
2022-11-26 03:55:45 +03:00
options = {
2022-11-17 22:53:58 +03:00
"title": text,
2022-11-26 03:55:45 +03:00
"body": text,
"limit": limit,
2023-10-05 21:46:18 +03:00
"offset": offset,
2022-11-17 22:53:58 +03:00
}
2023-10-25 21:33:53 +03:00
# FIXME: use elastic request here
2022-11-26 03:55:45 +03:00
payload = await load_shouts_by(None, None, options)
2022-11-17 23:29:04 +03:00
await redis.execute("SET", text, json.dumps(payload))
2022-11-17 22:53:58 +03:00
return payload
else:
return json.loads(cached)