From 8a585401627b6d9e63c4edbdc7a637b2ec650a56 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Mon, 27 Oct 2025 17:52:38 +0000 Subject: [PATCH 1/2] feat: add TTL index to MongoDB store for automatic expiration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a TTL index on the `expires_at` field in MongoDB collections to enable automatic cleanup of expired entries. MongoDB will delete expired documents approximately every 60 seconds using its built-in background process. This brings MongoDB's TTL handling in line with other stores like DynamoDB (which has native TTL) and Elasticsearch (which has manual cleanup). Benefits: - Automatic cleanup of expired entries without manual intervention - Reduced storage usage for expired data - Leverages MongoDB's native TTL index feature Related to #148 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: William Easton --- .../key-value-aio/src/key_value/aio/stores/mongodb/store.py | 4 ++++ .../src/key_value/sync/code_gen/stores/mongodb/store.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/key-value/key-value-aio/src/key_value/aio/stores/mongodb/store.py b/key-value/key-value-aio/src/key_value/aio/stores/mongodb/store.py index a98cfa5a..3cfde94a 100644 --- a/key-value/key-value-aio/src/key_value/aio/stores/mongodb/store.py +++ b/key-value/key-value-aio/src/key_value/aio/stores/mongodb/store.py @@ -174,8 +174,12 @@ async def _setup_collection(self, *, collection: str) -> None: new_collection: AsyncCollection[dict[str, Any]] = await self._db.create_collection(name=collection) + # Index for efficient key lookups _ = await new_collection.create_index(keys="key") + # TTL index for automatic expiration of entries when expires_at is reached + _ = await new_collection.create_index(keys="expires_at", expireAfterSeconds=0) + self._collections_by_name[collection] = new_collection @override diff --git a/key-value/key-value-sync/src/key_value/sync/code_gen/stores/mongodb/store.py b/key-value/key-value-sync/src/key_value/sync/code_gen/stores/mongodb/store.py index b82befb8..fafe9bf2 100644 --- a/key-value/key-value-sync/src/key_value/sync/code_gen/stores/mongodb/store.py +++ b/key-value/key-value-sync/src/key_value/sync/code_gen/stores/mongodb/store.py @@ -181,8 +181,12 @@ def _setup_collection(self, *, collection: str) -> None: new_collection: Collection[dict[str, Any]] = self._db.create_collection(name=collection) + # Index for efficient key lookups _ = new_collection.create_index(keys="key") + # TTL index for automatic expiration of entries when expires_at is reached + _ = new_collection.create_index(keys="expires_at", expireAfterSeconds=0) + self._collections_by_name[collection] = new_collection @override From 22c1cf98ae47a7f824a1e6c9b26489ce45f38540 Mon Sep 17 00:00:00 2001 From: William Easton Date: Tue, 28 Oct 2025 12:19:38 -0500 Subject: [PATCH 2/2] Lint --- .../key-value-sync/src/key_value/sync/code_gen/stores/base.py | 1 - 1 file changed, 1 deletion(-) diff --git a/key-value/key-value-sync/src/key_value/sync/code_gen/stores/base.py b/key-value/key-value-sync/src/key_value/sync/code_gen/stores/base.py index e57cc10c..1c02abda 100644 --- a/key-value/key-value-sync/src/key_value/sync/code_gen/stores/base.py +++ b/key-value/key-value-sync/src/key_value/sync/code_gen/stores/base.py @@ -252,7 +252,6 @@ def _put_managed_entries( created_at: datetime, expires_at: datetime | None, ) -> None: - """Store multiple managed entries by key in the specified collection. Args: