From 7149f8c45b286a580ee242098d9324ecd3e3bdbb Mon Sep 17 00:00:00 2001 From: hajdul88 <52442977+hajdul88@users.noreply.github.com> Date: Thu, 16 Oct 2025 16:13:55 +0200 Subject: [PATCH] ruff format --- .../databases/cache/redis/RedisAdapter.py | 30 ++++++++++--------- .../databases/exceptions/exceptions.py | 2 +- .../modules/retrieval/utils/session_cache.py | 16 ++++++---- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/cognee/infrastructure/databases/cache/redis/RedisAdapter.py b/cognee/infrastructure/databases/cache/redis/RedisAdapter.py index cc7984b32..c9e86640e 100644 --- a/cognee/infrastructure/databases/cache/redis/RedisAdapter.py +++ b/cognee/infrastructure/databases/cache/redis/RedisAdapter.py @@ -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) diff --git a/cognee/infrastructure/databases/exceptions/exceptions.py b/cognee/infrastructure/databases/exceptions/exceptions.py index dec3172a4..8d7300b16 100644 --- a/cognee/infrastructure/databases/exceptions/exceptions.py +++ b/cognee/infrastructure/databases/exceptions/exceptions.py @@ -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. """ diff --git a/cognee/modules/retrieval/utils/session_cache.py b/cognee/modules/retrieval/utils/session_cache.py index d0fe2da08..68146264d 100644 --- a/cognee/modules/retrieval/utils/session_cache.py +++ b/cognee/modules/retrieval/utils/session_cache.py @@ -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