This repository has been archived on 2023-10-01. You can view files and clone it, but cannot push or open issues or pull requests.
PyNyaaTa/pynyaata/cache/__init__.py
2023-01-04 22:32:27 +01:00

43 lines
854 B
Python

from functools import wraps
from logging import error
from os import getenv
from typing import Optional
from pynyaata.cache.simple import SimpleCache
from pynyaata.types import Cache
from redis import RedisError
REDIS_URL: Optional[str] = getenv("REDIS_URL")
client: Cache = SimpleCache()
if REDIS_URL:
try:
from pynyaata.cache.redis import RedisCache
client = RedisCache()
except RedisError as e:
error(e)
def cache_data(f):
@wraps(f)
async def wrapper(*args, **kwargs):
bridge = args[0]
query = args[1]
page = args[2]
key = f"pynyaata.{bridge.__class__.__name__}.{f.__name__}.{query}.{page}"
ret = client.get(key)
if ret:
return ret
ret = await f(*args, **kwargs)
client.set(key, ret)
return ret
return wrapper