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/redis.py
2022-12-22 00:01:21 +00:00

23 lines
601 B
Python

from json import dumps, loads
from typing import List, Optional
from pynyaata.cache import CACHE_TIMEOUT, REDIS_URL
from pynyaata.types import Cache, RemoteFile
from redis import ConnectionError, Redis
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)