From 395b1c289afea8c2524387c388599331368520b9 Mon Sep 17 00:00:00 2001 From: hajdul88 <52442977+hajdul88@users.noreply.github.com> Date: Tue, 7 Oct 2025 15:56:47 +0200 Subject: [PATCH] feat: makes redis import optional --- .../infrastructure/databases/cache/config.py | 2 -- .../databases/cache/get_cache_engine.py | 25 +++++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/cognee/infrastructure/databases/cache/config.py b/cognee/infrastructure/databases/cache/config.py index c99b91b9a..3db1b4f79 100644 --- a/cognee/infrastructure/databases/cache/config.py +++ b/cognee/infrastructure/databases/cache/config.py @@ -1,5 +1,3 @@ -import os -import pydantic from pydantic_settings import BaseSettings, SettingsConfigDict from functools import lru_cache diff --git a/cognee/infrastructure/databases/cache/get_cache_engine.py b/cognee/infrastructure/databases/cache/get_cache_engine.py index e14fdb31c..92186f877 100644 --- a/cognee/infrastructure/databases/cache/get_cache_engine.py +++ b/cognee/infrastructure/databases/cache/get_cache_engine.py @@ -3,9 +3,10 @@ from functools import lru_cache from cognee.infrastructure.databases.cache.config import get_cache_config -from cognee.infrastructure.databases.cache.redis.RedisAdapter import RedisAdapter from cognee.infrastructure.databases.cache.cache_db_interface import CacheDBInterface +config = get_cache_config() + @lru_cache def create_cache_engine( @@ -14,7 +15,7 @@ def create_cache_engine( lock_key: str, agentic_lock_expire: int = 240, agentic_lock_timeout: int = 300, -) -> CacheDBInterface: +): """ Factory function to instantiate a cache coordination backend (currently Redis). @@ -30,21 +31,25 @@ def create_cache_engine( -------- - CacheDBInterface: An instance of the appropriate cache adapter. :TODO: Now we support only Redis. later if we add more here we can split the logic """ + if config.caching: + from cognee.infrastructure.databases.cache.redis.RedisAdapter import RedisAdapter - return RedisAdapter( - host=cache_host, - port=cache_port, - lock_name=lock_key, - timeout=agentic_lock_expire, - blocking_timeout=agentic_lock_timeout, - ) + return RedisAdapter( + host=cache_host, + port=cache_port, + lock_name=lock_key, + timeout=agentic_lock_expire, + blocking_timeout=agentic_lock_timeout, + ) + else: + return None def get_cache_engine(lock_key: str) -> CacheDBInterface: """ Returns a cache adapter instance using current context configuration. """ - config = get_cache_config() + return create_cache_engine( cache_host=config.cache_host, cache_port=config.cache_port,