40 lines
903 B
Python
40 lines
903 B
Python
import orjson
|
|
|
|
from redis.asyncio import Redis
|
|
|
|
from nyekiapi.lib import settings
|
|
|
|
|
|
redis = Redis.from_url(settings.REDIS_URL)
|
|
|
|
|
|
async def get(key: str) -> str | None:
|
|
"""Retrieve a value from Redis by key.
|
|
|
|
Args:
|
|
key (str): The key to retrieve the value for.
|
|
|
|
Returns:
|
|
str | None: The value associated with the key, or None if the key does not exist.
|
|
"""
|
|
|
|
raw = await redis.get(key)
|
|
|
|
if raw:
|
|
return orjson.loads(raw)
|
|
|
|
else:
|
|
return None
|
|
|
|
|
|
async def set(key: str, value: str, expires_in: int | None = None) -> None:
|
|
"""Set a value in Redis with an optional expiration time.
|
|
|
|
Args:
|
|
key (str): The key to set the value for.
|
|
value (str): The value to set.
|
|
expires_in (int | None, optional): The expiration time in seconds. Defaults to None.
|
|
"""
|
|
|
|
await redis.set(key, orjson.dumps(value), ex=expires_in)
|