mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-09-11 20:27:54 +02:00
Overload clear method to delete all when called without args
This commit is contained in:
+15
-12
@@ -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:
|
||||
|
||||
+6
-4
@@ -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."""
|
||||
|
||||
+13
-8
@@ -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)
|
||||
|
||||
@@ -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__"),)
|
||||
)
|
||||
|
||||
|
||||
@@ -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]]:
|
||||
|
||||
Reference in New Issue
Block a user