From 6b78bcd857d0da07632b4ff98333fc5bd56ac052 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Fri, 2 May 2025 10:39:35 -0700 Subject: [PATCH] Implement ttl in FileCache --- .../langgraph/cache/file/__init__.py | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/libs/checkpoint/langgraph/cache/file/__init__.py b/libs/checkpoint/langgraph/cache/file/__init__.py index 2ed206abf..e586c1355 100644 --- a/libs/checkpoint/langgraph/cache/file/__init__.py +++ b/libs/checkpoint/langgraph/cache/file/__init__.py @@ -1,4 +1,5 @@ import asyncio +import datetime import dbm import ormsgpack @@ -22,11 +23,16 @@ class FileCache(BaseCache): def get(self, keys: list[str]) -> dict[str, bytes]: """Get the cached values for the given keys.""" - return { - key: self.serde.loads_typed(ormsgpack.unpackb(self._db[key])) - for key in keys - if key in self._db - } + now = datetime.datetime.now(datetime.timezone.utc).timestamp() + values: dict[str, bytes] = {} + for key in keys: + if val := self._db.get(key): + expiry, *data = ormsgpack.unpackb(val) + if expiry is not None and now > expiry: + self._db.pop(key, None) + continue + values[key] = self.serde.loads_typed(data) + return values async def aget(self, keys: list[str]) -> dict[str, bytes]: """Asynchronously get the cached values for the given keys.""" @@ -34,9 +40,14 @@ class FileCache(BaseCache): def set(self, mapping: dict[str, tuple[bytes, int | None]]) -> None: """Set the cached values for the given keys and TTLs.""" - for key, (value, _) in mapping.items(): - # File-based caches do not support TTLs, so we ignore them. - self._db[key] = ormsgpack.packb(self.serde.dumps_typed(value)) + now = datetime.datetime.now(datetime.timezone.utc) + for key, (value, ttl) in mapping.items(): + if ttl is not None: + delta = datetime.timedelta(seconds=ttl) + expiry: float | None = (now + delta).timestamp() + else: + expiry = None + self._db[key] = ormsgpack.packb((expiry, *self.serde.dumps_typed(value))) async def aset(self, mapping: dict[str, tuple[bytes, int | None]]) -> None: """Asynchronously set the cached values for the given keys and TTLs."""