Client-side query caching for PyMongo
Speed up read-heavy, high-throughput Python applications. client-query-cache serves eligible repeated PyMongo reads from memory, reducing read latency and avoiding MongoDB round trips on cache hits.
Add caching through an explicit cached collection view. Keep your existing MongoDB queries and use PyMongo for writes.
- Synchronous and asyncio APIs for long-lived Python applications.
- MongoDB change streams invalidate affected results, including writes from other applications.
- Shared caching across application workers on one supported Linux host reduces duplicate cached documents and uses one change stream per active database.
- A Rust-backed shared cache serves eligible warm reads from native shared memory without a MongoDB round trip.
Choose CacheManager for caching within one process, or SharedCacheManager to reuse eligible collection reads across trusted same-host workers. Both support sync and asyncio. See which manager to choose.
With shared caching, workers look up the same cache and use their own PyMongo clients for misses and bypasses:
flowchart TB
subgraph Application["Python application"]
Worker1["Python worker 1"] -->|"Cache lookup"| Cache["Rust-backed<br/>shared cache"]
Worker2["Python worker 2"] -->|"Cache lookup"| Cache
end
Worker1 -->|"Miss / bypass"| DB[("MongoDB")]
Worker2 -->|"Miss / bypass"| DB
DB -.->|"Change streams<br/>Async invalidation"| Cache
Benchmarks compare direct reads, independent caches and shared caching. Sharing avoids duplicated values and streams, but its owner process and cache misses have costs. Rust does not guarantee a faster application or lower total memory for every workload.
The graphic shows process-local measurements from local and remote MongoDB deployments; it is separate from the shared-cache comparison above.
Quick start
uv add client-query-cache
Or use pip install client-query-cache.
Start a MongoDB server before running this example. For local development, follow the single-member replica-set setup.
from pymongo import MongoClient
from client_query_cache import CacheManager
with (
MongoClient("mongodb://localhost:27017") as client,
CacheManager(client) as cache_manager,
):
collection = client["client_query_cache_tutorial"]["items"]
cached_collection = cache_manager.get_cached_collection(collection)
collection.replace_one(
{"_id": "example"}, {"_id": "example", "value": 42}, upsert=True
)
cached_collection.find_one({"_id": "example"})
cached_collection.find_one({"_id": "example"}) # Repeated reads can hit the cache.
Check the platform and MongoDB requirements before installing. Read eligibility determines which requests can use the cache.
Eligible cache misses read from the primary with majority read concern; collections configured with another read preference or read concern bypass the cache. Invalidation is asynchronous, with no guaranteed delay: after a write, cached reads can return the preceding value until its change event is processed. There is no time-to-live, entry-age limit or manual invalidation call. Use direct PyMongo reads with suitable consistency controls for read-after-write and current authorization; see consistency.
Run an application
Start with the synchronous or asyncio tutorial. The shared FastAPI example runs real Granian workers and verifies cross-process hits, invalidation and graceful shutdown. The compact FastAPI example demonstrates process-local caching.
Deployment · API reference · Examples
The library is tested against real MongoDB replica sets, including concurrency stress tests. Separate checks guard against performance and memory regressions.
MongoDB is a registered trademark of MongoDB, Inc. This project is independent and is not affiliated with or endorsed by MongoDB, Inc.
Metadata
Release files for client-query-cache 0.14.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| client_query_cache-0.14.1.tar.gz | 154.5 kB | With provenance | Details for client_query_cache-0.14.1.tar.gz |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| client_query_cache-0.14.1-cp314-abi3-manylinux_2_28_x86_64.whl | CPython 3.14 | abi3 | Linux glibc 2.28+ x86-64 | Details for client_query_cache-0.14.1-cp314-abi3-manylinux_2_28_x86_64.whl |
| client_query_cache-0.14.1-cp314-abi3-manylinux_2_28_aarch64.whl | CPython 3.14 | abi3 | Linux glibc 2.28+ ARM64 | Details for client_query_cache-0.14.1-cp314-abi3-manylinux_2_28_aarch64.whl |
Total release size: 1.5 MB
Release files / client_query_cache-0.14.1.tar.gz
| Download URL | client_query_cache-0.14.1.tar.gz |
|---|---|
| Size | 154.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f5c6c340887a48dcbab028b2880716d6154202daddbe48be8ec84152c673b2f9
|
|
BLAKE2b-256 checksum How to use checksums |
cd04e357509860304f1cec9b390e8b0b2042661bfaa7431fc9774765495c3c94
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logRelease files / client_query_cache-0.14.1-cp314-abi3-manylinux_2_28_x86_64.whl
| Download URL | client_query_cache-0.14.1-cp314-abi3-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 677.1 kB |
| Tags | CPython 3.14 Linux glibc 2.28+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
99ee35cfb3b8c08611a8572974c82d5525c0887703c1997b4d8b8f3de6ef030e
|
|
BLAKE2b-256 checksum How to use checksums |
8348cf567614e5a8e910cada4f03cf1dae7311b89dde13c55754bdffaf250a52
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logRelease files / client_query_cache-0.14.1-cp314-abi3-manylinux_2_28_aarch64.whl
| Download URL | client_query_cache-0.14.1-cp314-abi3-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 653.9 kB |
| Tags | CPython 3.14 Linux glibc 2.28+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
32a5656cdfedd62df72b7e1e86064ba65bd0e7df79e04a9ea054ecd36197a603
|
|
BLAKE2b-256 checksum How to use checksums |
00ad717349a5a314f388a5384eea41b80ac002c317233b51e030528efef99f53
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logSecurity for client-query-cache 0.14.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 3 files in this release have been uploaded with provenance metadata.
Attestation for client_query_cache-0.14.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.