diff --git a/libs/checkpoint-sqlite/langgraph/cache/sqlite/__init__.py b/libs/checkpoint-sqlite/langgraph/cache/sqlite/__init__.py index 9942a1228..258006c54 100644 --- a/libs/checkpoint-sqlite/langgraph/cache/sqlite/__init__.py +++ b/libs/checkpoint-sqlite/langgraph/cache/sqlite/__init__.py @@ -95,20 +95,23 @@ class SqliteCache(BaseCache[ValueT]): """Asynchronously set the cached values for the given keys and TTLs.""" await asyncio.to_thread(self.set, mapping) - def delete(self, keys: Sequence[Namespace]) -> None: - """Delete the cached values for the given namespaces.""" - if not keys: - return + def clear(self, namespaces: Sequence[Namespace] | None = None) -> None: + """Delete the cached values for the given namespaces. + If no namespaces are provided, clear all cached values.""" with self._lock, self._conn: - placeholders = ",".join("?" for _ in keys) - self._conn.execute( - f"DELETE FROM cache WHERE (ns) IN ({placeholders})", - tuple(",".join(key) for key in keys), - ) + if namespaces is None: + self._conn.execute("DELETE FROM cache") + else: + placeholders = ",".join("?" for _ in namespaces) + self._conn.execute( + f"DELETE FROM cache WHERE (ns) IN ({placeholders})", + tuple(",".join(key) for key in namespaces), + ) - async def adelete(self, keys: Sequence[Namespace]) -> None: - """Asynchronously delete the cached values for the given namespaces.""" - await asyncio.to_thread(self.delete, keys) + async def aclear(self, namespaces: Sequence[Namespace] | None = None) -> None: + """Asynchronously delete the cached values for the given namespaces. + If no namespaces are provided, clear all cached values.""" + await asyncio.to_thread(self.delete, namespaces) def __del__(self) -> None: try: diff --git a/libs/checkpoint/langgraph/cache/base/__init__.py b/libs/checkpoint/langgraph/cache/base/__init__.py index 8f8660cca..c23e859fd 100644 --- a/libs/checkpoint/langgraph/cache/base/__init__.py +++ b/libs/checkpoint/langgraph/cache/base/__init__.py @@ -38,9 +38,11 @@ class BaseCache(ABC, Generic[ValueT]): """Asynchronously set the cached values for the given keys and TTLs.""" @abstractmethod - def delete(self, keys: Sequence[Namespace]) -> None: - """Delete the cached values for the given keys.""" + def clear(self, namespaces: Sequence[Namespace] | None = None) -> None: + """Delete the cached values for the given namespaces. + If no namespaces are provided, clear all cached values.""" @abstractmethod - async def adelete(self, keys: Sequence[Namespace]) -> None: - """Asynchronously delete the cached values for the given keys.""" + async def aclear(self, namespaces: Sequence[Namespace] | None = None) -> None: + """Asynchronously delete the cached values for the given namespaces. + If no namespaces are provided, clear all cached values.""" diff --git a/libs/checkpoint/langgraph/cache/memory/__init__.py b/libs/checkpoint/langgraph/cache/memory/__init__.py index a6d858e80..7b10db051 100644 --- a/libs/checkpoint/langgraph/cache/memory/__init__.py +++ b/libs/checkpoint/langgraph/cache/memory/__init__.py @@ -56,13 +56,18 @@ class InMemoryCache(BaseCache[ValueT]): """Asynchronously set the cached values for the given keys.""" self.set(keys) - def delete(self, keys: Sequence[Namespace]) -> None: - """Delete the cached values for the given namespaces.""" + def clear(self, namespaces: Sequence[Namespace] | None = None) -> None: + """Delete the cached values for the given namespaces. + If no namespaces are provided, clear all cached values.""" with self._lock: - for ns in keys: - if ns in self._cache: - del self._cache[ns] + if namespaces is None: + self._cache.clear() + else: + for ns in namespaces: + if ns in self._cache: + del self._cache[ns] - async def adelete(self, keys: Sequence[Namespace]) -> None: - """Asynchronously delete the cached values for the given namespaces.""" - self.delete(keys) + async def aclear(self, namespaces: Sequence[Namespace] | None = None) -> None: + """Asynchronously delete the cached values for the given namespaces. + If no namespaces are provided, clear all cached values.""" + self.clear(namespaces) diff --git a/libs/langgraph/langgraph/func/__init__.py b/libs/langgraph/langgraph/func/__init__.py index 76599c671..143b1a2de 100644 --- a/libs/langgraph/langgraph/func/__init__.py +++ b/libs/langgraph/langgraph/func/__init__.py @@ -69,12 +69,12 @@ class TaskFunction(Generic[P, T]): def clear_cache(self, cache: BaseCache) -> None: """Clear the cache for this task.""" if self.cache_policy is not None: - cache.delete(((CACHE_NS_WRITES, identifier(self.func) or "__dynamic__"),)) + cache.clear(((CACHE_NS_WRITES, identifier(self.func) or "__dynamic__"),)) async def aclear_cache(self, cache: BaseCache) -> None: """Clear the cache for this task.""" if self.cache_policy is not None: - await cache.adelete( + await cache.aclear( ((CACHE_NS_WRITES, identifier(self.func) or "__dynamic__"),) ) diff --git a/libs/langgraph/langgraph/pregel/__init__.py b/libs/langgraph/langgraph/pregel/__init__.py index 286a98ddc..fe4f2c833 100644 --- a/libs/langgraph/langgraph/pregel/__init__.py +++ b/libs/langgraph/langgraph/pregel/__init__.py @@ -3008,7 +3008,7 @@ class Pregel(PregelProtocol): ), ) # clear cache - self.cache.delete(namespaces) + self.cache.clear(namespaces) async def aclear_cache(self, nodes: Sequence[str] | None = None) -> None: """Asynchronously clear the cache for the given nodes.""" @@ -3027,7 +3027,7 @@ class Pregel(PregelProtocol): ), ) # clear cache - await self.cache.adelete(namespaces) + await self.cache.aclear(namespaces) def _trigger_to_nodes(nodes: dict[str, PregelNode]) -> Mapping[str, Sequence[str]]: