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

View file

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

View file

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