40 lines
831 B
Python
40 lines
831 B
Python
from functools import wraps
|
|
from logging import error
|
|
from os import getenv
|
|
from typing import Optional
|
|
|
|
from pynyaata2.cache.simple import SimpleCache
|
|
from pynyaata2.types import Bridge, Cache
|
|
|
|
from redis import RedisError
|
|
|
|
|
|
REDIS_URL: Optional[str] = getenv("REDIS_URL")
|
|
|
|
client: Cache = SimpleCache()
|
|
|
|
if REDIS_URL:
|
|
try:
|
|
from pynyaata2.cache.redis import RedisCache
|
|
|
|
client = RedisCache()
|
|
except RedisError as e:
|
|
error(e)
|
|
|
|
|
|
def cache_data(f):
|
|
@wraps(f)
|
|
def wrapper(bridge: Bridge, query: str = "", page: int = 1):
|
|
key = f"pynyaata.{bridge.__class__.__name__}.{f.__name__}.{query}.{page}"
|
|
cached = client.get(key)
|
|
|
|
if cached:
|
|
return cached
|
|
|
|
reals = f(bridge, query, page)
|
|
client.set(key, reals)
|
|
|
|
return reals
|
|
|
|
return wrapper
|