26 lines
674 B
Python
26 lines
674 B
Python
from json import dumps, loads
|
|
from os import getenv
|
|
from typing import List, Optional
|
|
|
|
from pynyaata2.types import Cache, RemoteFile
|
|
|
|
from redis import ConnectionError, Redis
|
|
|
|
|
|
CACHE_TIMEOUT = int(getenv("CACHE_TIMEOUT", 60 * 60))
|
|
REDIS_URL: Optional[str] = getenv("REDIS_URL")
|
|
|
|
if not REDIS_URL:
|
|
raise ConnectionError(f"Invalid REDIS_URL: {REDIS_URL}")
|
|
|
|
client = Redis.from_url(REDIS_URL)
|
|
|
|
|
|
class RedisCache(Cache):
|
|
def get(self, key: str) -> Optional[List[RemoteFile]]:
|
|
data = client.get(key)
|
|
return loads(str(data)) if data else None
|
|
|
|
def set(self, key: str, data: List[RemoteFile]):
|
|
return client.set(key, dumps(data), CACHE_TIMEOUT)
|