~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
refactor: Simplify cache system from two-tier to single-tier
LearningCircuit2d0c42f96174
diff --git a/src/local_deep_research/memory_cache/config.py b/src/local_deep_research/memory_cache/config.pyindex af6e842aed..31cf52a4d2 100644--- a/src/local_deep_research/memory_cache/config.py+++ b/src/local_deep_research/memory_cache/config.py@@ -166,31 +166,42 @@ def create_null_cache_region() -> CacheRegion:return region-class TwoTierCache:- """Two-tier memory caching system with different TTLs (hot: 5min, warm: 1hr)."""+class SingleTierCache:+ """Simple single-tier in-memory cache with configurable TTL."""def __init__(self,- hot_region: Optional[CacheRegion] = None,- warm_region: Optional[CacheRegion] = None,+ cache_region: Optional[CacheRegion] = None,enable_metrics: bool = True,+ default_ttl: int = DEFAULT_WARM_CACHE_TTL,):- """Initialize two-tier cache.+ """Initialize single-tier cache.Args:- hot_region: In-memory cache region (5 minute TTL)- warm_region: In-memory cache region (1 hour TTL)+ cache_region: In-memory cache region (defaults to 1 hour TTL)enable_metrics: Whether to track cache metrics+ default_ttl: Default TTL in seconds if no cache_region provided"""- self.hot = hot_region or create_hot_cache_region()- self.warm = warm_region or create_warm_cache_region()- self.enable_metrics = enable_metrics+ if cache_region:+ self.cache = cache_region+ else:+ # Create a single cache with moderate TTL (1 hour by default)+ self.cache = make_region()+ self.cache.configure(+ "dogpile.cache.memory",+ expiration_time=default_ttl,+ arguments={+ "cache_dict": {},+ "max_size": 10000, # Reasonable size for in-memory cache+ },+ )- # Metrics tracking- self.metrics = {"hot_hits": 0, "warm_hits": 0, "misses": 0, "errors": 0}+ self.enable_metrics = enable_metrics+ # Simplified metrics+ self.metrics = {"hits": 0, "misses": 0, "errors": 0}def get(self, user_id: str, namespace: str, key: str) -> Any:- """Get value from cache with two-tier lookup.+ """Get value from cache.Args:user_id: User identifier@@ -202,32 +213,14 @@ class TwoTierCache:"""cache_key = get_namespaced_key(user_id, namespace, key)- # Try hot cache firsttry:- value = self.hot.get(cache_key)+ value = self.cache.get(cache_key)if value is not NO_VALUE:if self.enable_metrics:- self.metrics["hot_hits"] += 1+ self.metrics["hits"] += 1return valueexcept Exception as e:- logger.warning(f"Hot cache error: {e}")- if self.enable_metrics:- self.metrics["errors"] += 1-- # Try warm cache- try:- value = self.warm.get(cache_key)- if value is not NO_VALUE:- if self.enable_metrics:- self.metrics["warm_hits"] += 1- # Promote to hot cache- try:- self.hot.set(cache_key, value)- except Exception:- pass # Non-critical- return value- except Exception as e:- logger.warning(f"Warm cache error: {e}")+ logger.warning(f"Cache error: {e}")if self.enable_metrics:self.metrics["errors"] += 1@@ -244,28 +237,23 @@ class TwoTierCache:value: Any,expiration_time: Optional[int] = None,) -> None:- """Set value in both cache tiers.+ """Set value in cache.Args:user_id: User identifiernamespace: Cache namespacekey: Cache keyvalue: Value to cache- expiration_time: Optional TTL override (ignored - uses region TTL)+ expiration_time: Optional TTL override (currently ignored - uses region TTL)"""cache_key = get_namespaced_key(user_id, namespace, key)- # Set in both tiers (expiration is configured at region level)- try:- self.hot.set(cache_key, value)- except Exception as e:- logger.warning(f"Failed to set hot cache: {e}")-try:- # Note: expiration_time is ignored - it's set at region level- self.warm.set(cache_key, value)+ self.cache.set(cache_key, value)except Exception as e:- logger.warning(f"Failed to set warm cache: {e}")+ logger.warning(f"Failed to set cache: {e}")+ if self.enable_metrics:+ self.metrics["errors"] += 1def invalidate(self,@@ -338,11 +326,11 @@ class TwoTierCache:# Exact match or complex patternreturn fnmatch.fnmatch(key_suffix, pattern)- # Invalidate in hot cache+ # Invalidate in cachetry:# Access the underlying cache dict for memory backend- if hasattr(self.hot.backend, "_cache"):- cache_dict = self.hot.backend._cache+ if hasattr(self.cache.backend, "_cache"):+ cache_dict = self.cache.backend._cachekeys_to_delete = [kfor k in cache_dict.keys()@@ -351,28 +339,10 @@ class TwoTierCache:for key in keys_to_delete:del cache_dict[key]logger.debug(- f"Invalidated {len(keys_to_delete)} keys from hot cache"+ f"Invalidated {len(keys_to_delete)} keys from cache")except Exception as e:- logger.warning(f"Failed to invalidate hot cache pattern: {e}")-- # Invalidate in warm cache- try:- # Access the underlying cache dict for memory backend- if hasattr(self.warm.backend, "_cache"):- cache_dict = self.warm.backend._cache- keys_to_delete = [- k- for k in cache_dict.keys()- if matches_pattern(k, prefix, pattern)- ]- for key in keys_to_delete:- del cache_dict[key]- logger.debug(- f"Invalidated {len(keys_to_delete)} keys from warm cache"- )- except Exception as e:- logger.warning(f"Failed to invalidate warm cache pattern: {e}")+ logger.warning(f"Failed to invalidate cache pattern: {e}")def get_metrics(self) -> Dict[str, Union[int, float]]:"""Get cache performance metrics.@@ -380,35 +350,32 @@ class TwoTierCache:Returns:Dictionary of cache metrics"""- total_hits = self.metrics["hot_hits"] + self.metrics["warm_hits"]- total_requests = total_hits + self.metrics["misses"]+ total_requests = self.metrics["hits"] + self.metrics["misses"]return {**self.metrics,- "total_hits": total_hits,"total_requests": total_requests,- "hit_rate": total_hits / total_requests- if total_requests > 0- else 0,- "hot_hit_rate": self.metrics["hot_hits"] / total_requests+ "hit_rate": self.metrics["hits"] / total_requestsif total_requests > 0else 0,}-def get_thread_local_cache(redis_url: Optional[str] = None) -> TwoTierCache:+# Compatibility alias for existing code+TwoTierCache = SingleTierCache+++def get_thread_local_cache(redis_url: Optional[str] = None) -> SingleTierCache:"""Get or create thread-local cache instance.Args:redis_url: Optional Redis URL overrideReturns:- Thread-local TwoTierCache instance+ Thread-local SingleTierCache instance"""if not hasattr(thread_local, "cache"):- thread_local.cache = TwoTierCache(- warm_region=create_warm_cache_region(redis_url)- )+ thread_local.cache = SingleTierCache()return thread_local.cachediff --git a/src/local_deep_research/memory_cache/flask_integration.py b/src/local_deep_research/memory_cache/flask_integration.pyindex 2af6be11d0..9b8bcf4e82 100644--- a/src/local_deep_research/memory_cache/flask_integration.py+++ b/src/local_deep_research/memory_cache/flask_integration.py@@ -5,16 +5,14 @@ including app initialization, request hooks, and login handlers."""import asyncio-import osfrom flask import Flask, g, current_appfrom flask_login import user_loaded_from_requestfrom loguru import loggerfrom dogpile.cache.api import NO_VALUEfrom .config import (- TwoTierCache,- create_hot_cache_region,- create_warm_cache_region,+ SingleTierCache,+ TwoTierCache, # Alias for compatibility)from .warmup import get_cache_warmer@@ -26,33 +24,23 @@ def init_cache(app: Flask) -> None:app: Flask application instance"""# Get cache configuration from app config or environment- redis_url = app.config.get("CACHE_REDIS_URL", os.environ.get("REDIS_URL"))enable_cache = app.config.get("ENABLE_CACHE", True)if not enable_cache:logger.warning("Cache is disabled via configuration")- app.cache_regions = {"hot": None, "warm": None}app.cache = Nonereturn- # Create cache regions at app level+ # Create single-tier cache at app leveltry:- hot_region = create_hot_cache_region()- warm_region = create_warm_cache_region(redis_url)-- app.cache_regions = {"hot": hot_region, "warm": warm_region}-- # Create two-tier cache- app.cache = TwoTierCache(- hot_region=hot_region, warm_region=warm_region, enable_metrics=True- )+ # Create single-tier cache with 1 hour TTL+ app.cache = SingleTierCache(enable_metrics=True)logger.info("Cache initialized successfully")except Exception as e:- logger.error(f"Failed to initialize cache: {e}")+ logger.exception(f"Failed to initialize cache: {e}")# Fall back to null cache- app.cache_regions = {"hot": None, "warm": None}app.cache = None# Cache invalidation setup will be done per-user session@@ -141,7 +129,10 @@ def _register_login_handlers(app: Flask) -> None:loop.run_until_complete(warmer.warm_user_cache_async(str(user.id),- cache_types=["settings", "api_keys", "metrics"],+ cache_types=[+ "settings",+ "api_keys",+ ], # Removed metrics))except Exception as e: