ruff format

This commit is contained in:
hajdul88 2025-10-16 16:13:55 +02:00
parent fe1b02d937
commit 7149f8c45b
3 changed files with 27 additions and 21 deletions

View file

@ -28,31 +28,31 @@ class RedisAdapter(CacheDBInterface):
self.host = host
self.port = port
self.connection_timeout = connection_timeout
try:
self.sync_redis = redis.Redis(
host=host,
port=port,
username=username,
host=host,
port=port,
username=username,
password=password,
socket_connect_timeout=connection_timeout,
socket_timeout=connection_timeout,
)
self.async_redis = aioredis.Redis(
host=host,
port=port,
username=username,
password=password,
host=host,
port=port,
username=username,
password=password,
decode_responses=True,
socket_connect_timeout=connection_timeout,
)
self.timeout = timeout
self.blocking_timeout = blocking_timeout
# Validate connection on initialization
self._validate_connection()
logger.info(f"Successfully connected to Redis at {host}:{port}")
except (redis.ConnectionError, redis.TimeoutError) as e:
error_msg = f"Failed to connect to Redis at {host}:{port}: {str(e)}"
logger.error(error_msg)
@ -61,13 +61,15 @@ class RedisAdapter(CacheDBInterface):
error_msg = f"Unexpected error initializing Redis adapter: {str(e)}"
logger.error(error_msg)
raise CacheConnectionError(error_msg) from e
def _validate_connection(self):
"""Validate Redis connection is available."""
try:
self.sync_redis.ping()
except (redis.ConnectionError, redis.TimeoutError) as e:
raise CacheConnectionError(f"Cannot connect to Redis at {self.host}:{self.port}: {str(e)}") from e
raise CacheConnectionError(
f"Cannot connect to Redis at {self.host}:{self.port}: {str(e)}"
) from e
def acquire_lock(self):
"""
@ -127,7 +129,7 @@ class RedisAdapter(CacheDBInterface):
context: Context used to answer.
answer: Assistant answer text.
ttl: Optional time-to-live (seconds). If provided, the session expires after this time.
Raises:
CacheConnectionError: If Redis connection fails or times out.
"""
@ -145,7 +147,7 @@ class RedisAdapter(CacheDBInterface):
if ttl is not None:
await self.async_redis.expire(session_key, ttl)
except (redis.ConnectionError, redis.TimeoutError) as e:
error_msg = f"Redis connection error while adding Q&A: {str(e)}"
logger.error(error_msg)

View file

@ -137,7 +137,7 @@ class MutuallyExclusiveQueryParametersError(CogneeValidationError):
class CacheConnectionError(CogneeConfigurationError):
"""
Raised when connection to the cache database (e.g., Redis) fails.
This error indicates that the cache service is unavailable or misconfigured.
"""

View file

@ -15,7 +15,7 @@ async def save_to_session_cache(
) -> bool:
"""
Saves Q&A interaction to the session cache if user is authenticated and caching is enabled.
Handles cache unavailability gracefully by logging warnings instead of failing.
Parameters:
@ -46,7 +46,7 @@ async def save_to_session_cache(
from cognee.infrastructure.databases.cache.get_cache_engine import get_cache_engine
cache_engine = get_cache_engine()
if cache_engine is None:
logger.warning("Cache engine not available, skipping session save")
return False
@ -58,14 +58,18 @@ async def save_to_session_cache(
context=context_summary,
answer=answer,
)
logger.info(f"Successfully saved Q&A to session cache: user_id={user_id}, session_id={session_id}")
logger.info(
f"Successfully saved Q&A to session cache: user_id={user_id}, session_id={session_id}"
)
return True
except CacheConnectionError as e:
logger.warning(f"Cache unavailable, continuing without session save: {e.message}")
return False
except Exception as e:
logger.error(f"Unexpected error saving to session cache: {type(e).__name__}: {str(e)}. Continuing without caching.")
logger.error(
f"Unexpected error saving to session cache: {type(e).__name__}: {str(e)}. Continuing without caching."
)
return False