diff --git a/src/crewai/memory/storage/llm_response_cache_storage.py b/src/crewai/memory/storage/llm_response_cache_storage.py index 9a2e4cb25c..20110b6e36 100644 --- a/src/crewai/memory/storage/llm_response_cache_storage.py +++ b/src/crewai/memory/storage/llm_response_cache_storage.py @@ -23,7 +23,7 @@ def __init__( ) -> None: self.db_path = db_path self._printer: Printer = Printer() - self._connection_pool = {} + self._connection_pool: Dict[int, sqlite3.Connection] = {} self._initialize_db() def _get_connection(self) -> sqlite3.Connection: @@ -214,10 +214,27 @@ def cleanup_expired_cache(self, max_age_days: int = 7) -> None: """ Removes cache entries older than the specified number of days. + This method helps maintain the cache size and ensures that only recent + responses are kept, which is important for keeping the cache relevant + and preventing it from growing too large over time. + Args: max_age_days: Maximum age of cache entries in days. Defaults to 7. If set to 0, all entries will be deleted. + Must be a non-negative integer. + + Raises: + ValueError: If max_age_days is not a non-negative integer. """ + if not isinstance(max_age_days, int) or max_age_days < 0: + error_msg = "max_age_days must be a non-negative integer" + self._printer.print( + content=f"LLM RESPONSE CACHE ERROR: {error_msg}", + color="red", + ) + logger.error(error_msg) + raise ValueError(error_msg) + try: conn = self._get_connection() cursor = conn.cursor() @@ -228,10 +245,11 @@ def cleanup_expired_cache(self, max_age_days: int = 7) -> None: logger.info("Deleting all cache entries (max_age_days <= 0)") else: cursor.execute( - f""" - DELETE FROM llm_response_cache - WHERE timestamp < datetime('now', '-{max_age_days} days') """ + DELETE FROM llm_response_cache + WHERE timestamp < datetime('now', ? || ' days') + """, + (f"-{max_age_days}",) ) deleted_count = cursor.rowcount