Skip to content

Commit 681aa5e

Browse files
feat: add TTL index to MongoDB store for automatic expiration (#155)
Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com> Co-authored-by: William Easton <strawgate@users.noreply.github.com>
1 parent 131f11d commit 681aa5e

File tree

2 files changed

+8
-0
lines changed
  • key-value
    • key-value-aio/src/key_value/aio/stores/mongodb
    • key-value-sync/src/key_value/sync/code_gen/stores/mongodb

2 files changed

+8
-0
lines changed

key-value/key-value-aio/src/key_value/aio/stores/mongodb/store.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,12 @@ async def _setup_collection(self, *, collection: str) -> None:
174174

175175
new_collection: AsyncCollection[dict[str, Any]] = await self._db.create_collection(name=collection)
176176

177+
# Index for efficient key lookups
177178
_ = await new_collection.create_index(keys="key")
178179

180+
# TTL index for automatic expiration of entries when expires_at is reached
181+
_ = await new_collection.create_index(keys="expires_at", expireAfterSeconds=0)
182+
179183
self._collections_by_name[collection] = new_collection
180184

181185
@override

key-value/key-value-sync/src/key_value/sync/code_gen/stores/mongodb/store.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,12 @@ def _setup_collection(self, *, collection: str) -> None:
181181

182182
new_collection: Collection[dict[str, Any]] = self._db.create_collection(name=collection)
183183

184+
# Index for efficient key lookups
184185
_ = new_collection.create_index(keys="key")
185186

187+
# TTL index for automatic expiration of entries when expires_at is reached
188+
_ = new_collection.create_index(keys="expires_at", expireAfterSeconds=0)
189+
186190
self._collections_by_name[collection] = new_collection
187191

188192
@override

0 commit comments

Comments
 (0)