2022-10-26 13:06:04 +00:00
|
|
|
from functools import wraps
|
2023-01-04 21:32:27 +00:00
|
|
|
from logging import error
|
2022-12-22 00:01:21 +00:00
|
|
|
from os import getenv
|
|
|
|
from typing import Optional
|
|
|
|
|
2023-06-04 15:53:29 +00:00
|
|
|
from pynyaata2.cache.simple import SimpleCache
|
|
|
|
from pynyaata2.types import Bridge, Cache
|
2022-12-22 00:01:21 +00:00
|
|
|
|
2022-10-26 13:06:04 +00:00
|
|
|
from redis import RedisError
|
|
|
|
|
|
|
|
|
2022-12-22 00:01:21 +00:00
|
|
|
REDIS_URL: Optional[str] = getenv("REDIS_URL")
|
2022-10-26 13:06:04 +00:00
|
|
|
|
|
|
|
client: Cache = SimpleCache()
|
2022-12-22 00:01:21 +00:00
|
|
|
|
2022-10-26 13:06:04 +00:00
|
|
|
if REDIS_URL:
|
|
|
|
try:
|
2023-06-04 15:53:29 +00:00
|
|
|
from pynyaata2.cache.redis import RedisCache
|
2022-10-26 13:06:04 +00:00
|
|
|
|
|
|
|
client = RedisCache()
|
|
|
|
except RedisError as e:
|
2023-01-04 21:32:27 +00:00
|
|
|
error(e)
|
2022-10-26 13:06:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def cache_data(f):
|
|
|
|
@wraps(f)
|
2023-01-13 22:11:55 +00:00
|
|
|
def wrapper(bridge: Bridge, query: str = "", page: int = 1):
|
2023-01-04 15:57:16 +00:00
|
|
|
key = f"pynyaata.{bridge.__class__.__name__}.{f.__name__}.{query}.{page}"
|
2023-01-06 17:19:59 +00:00
|
|
|
cached = client.get(key)
|
2022-10-26 13:06:04 +00:00
|
|
|
|
2023-01-06 17:19:59 +00:00
|
|
|
if cached:
|
|
|
|
return cached
|
2022-10-26 13:06:04 +00:00
|
|
|
|
2023-01-13 22:11:55 +00:00
|
|
|
reals = f(bridge, query, page)
|
2023-01-06 17:19:59 +00:00
|
|
|
client.set(key, reals)
|
2022-10-26 13:06:04 +00:00
|
|
|
|
2023-01-06 17:19:59 +00:00
|
|
|
return reals
|
2022-10-26 13:06:04 +00:00
|
|
|
|
|
|
|
return wrapper
|