38 lines
825 B
Python
38 lines
825 B
Python
|
import logging
|
||
|
from functools import wraps
|
||
|
from os import environ
|
||
|
from pynyaata.cache.simple import SimpleCache
|
||
|
from pynyaata.types import Cache
|
||
|
from redis import RedisError
|
||
|
|
||
|
|
||
|
CACHE_TIMEOUT = int(environ.get("CACHE_TIMEOUT", 60 * 60))
|
||
|
REDIS_URL = environ.get("REDIS_URL", "")
|
||
|
|
||
|
client: Cache = SimpleCache()
|
||
|
if REDIS_URL:
|
||
|
try:
|
||
|
from pynyaata.cache.redis import RedisCache
|
||
|
|
||
|
client = RedisCache()
|
||
|
except RedisError as e:
|
||
|
logging.error(e)
|
||
|
|
||
|
|
||
|
def cache_data(f):
|
||
|
@wraps(f)
|
||
|
def wrapper(*args, **kwds):
|
||
|
bridge = args[0]
|
||
|
key = f"pynyaata.{bridge.__class__.__name__}.{f.__name__}.{bridge.query}.{bridge.page}"
|
||
|
ret = client.get(key)
|
||
|
|
||
|
if ret:
|
||
|
return ret
|
||
|
|
||
|
ret = f(*args, **kwds)
|
||
|
client.set(key, ret)
|
||
|
|
||
|
return ret
|
||
|
|
||
|
return wrapper
|