011 fix redis
This commit is contained in:
+1
-1
@@ -13,7 +13,7 @@ from pydantic_settings import (
|
||||
YamlConfigSettingsSource,
|
||||
)
|
||||
|
||||
DEFAULT_CONFIG_FILE = "config.yaml"
|
||||
DEFAULT_CONFIG_FILE = "/config.yaml"
|
||||
TEST_CONFIG_FILE = "config.test.yaml"
|
||||
|
||||
|
||||
|
||||
+20
-7
@@ -19,6 +19,10 @@ class RedisClientProtocol(Protocol):
|
||||
async def delete(self, key: str) -> Any: ...
|
||||
|
||||
|
||||
class PriceCacheRepositoryError(RuntimeError):
|
||||
"""Deterministic repository error raised on Redis operation failures."""
|
||||
|
||||
|
||||
class PriceCache:
|
||||
"""Repository for cached provider payloads."""
|
||||
|
||||
@@ -41,7 +45,10 @@ class PriceCache:
|
||||
)
|
||||
|
||||
async def get(self, key: str) -> Any | None:
|
||||
payload = await self._redis_client.get(key)
|
||||
try:
|
||||
payload = await self._redis_client.get(key)
|
||||
except Exception as exc:
|
||||
raise PriceCacheRepositoryError("Redis cache read failed.") from exc
|
||||
if payload is None:
|
||||
return None
|
||||
|
||||
@@ -57,27 +64,33 @@ class PriceCache:
|
||||
async def set(self, key: str, value: Any, ttl: int | None = None) -> None:
|
||||
serialized_payload = _serialize(value)
|
||||
resolved_ttl = self._ttl_seconds if ttl is None else ttl
|
||||
await self._redis_client.set(key, serialized_payload, ex=resolved_ttl)
|
||||
try:
|
||||
await self._redis_client.set(key, serialized_payload, ex=resolved_ttl)
|
||||
except Exception as exc:
|
||||
raise PriceCacheRepositoryError("Redis cache write failed.") from exc
|
||||
|
||||
async def invalidate(self, key: str) -> None:
|
||||
await self._redis_client.delete(key)
|
||||
try:
|
||||
await self._redis_client.delete(key)
|
||||
except Exception as exc:
|
||||
raise PriceCacheRepositoryError("Redis cache invalidate failed.") from exc
|
||||
|
||||
|
||||
def _default_redis_client_factory(redis_dsn: str) -> RedisClientProtocol:
|
||||
"""Create async Redis client from DSN."""
|
||||
|
||||
try:
|
||||
import redis.asyncio as redis_asyncio # type: ignore[import-not-found]
|
||||
from aioredis import from_url as aioredis_from_url # type: ignore
|
||||
except ModuleNotFoundError:
|
||||
try:
|
||||
from aioredis import from_url as aioredis_from_url # type: ignore
|
||||
import redis.asyncio as redis_asyncio # type: ignore[import-not-found]
|
||||
except ModuleNotFoundError as exc:
|
||||
raise RuntimeError(
|
||||
"No async Redis client implementation is available."
|
||||
) from exc
|
||||
return aioredis_from_url(redis_dsn, encoding="utf-8", decode_responses=False)
|
||||
return redis_asyncio.from_url(redis_dsn, encoding="utf-8", decode_responses=False)
|
||||
|
||||
return redis_asyncio.from_url(redis_dsn, encoding="utf-8", decode_responses=False)
|
||||
return aioredis_from_url(redis_dsn, encoding="utf-8", decode_responses=False)
|
||||
|
||||
|
||||
def _serialize(value: Any) -> str:
|
||||
|
||||
Reference in New Issue
Block a user