Skip to main content

Client-side query caching for PyMongo

PyPI Python Coverage Typing

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.

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.

Illustrative process-local read latency on a logarithmic scale: direct local MongoDB read, about 120 microseconds; direct Atlas M0 read, 79.4 milliseconds; cached reads in either deployment, about 61 microseconds.

The graphic shows process-local measurements from local MongoDB and Atlas deployments; it is separate from the shared-cache comparison above.

Quick start

uv add client-query-cache

Or use pip install client-query-cache.

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.

Invalidation is asynchronous: cached reads can briefly return a preceding value after a write. 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.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for client-query-cache 0.9.0
File Size Uploaded Provenance Actions
client_query_cache-0.9.0.tar.gz 114.9 kB With provenance Details for client_query_cache-0.9.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for client-query-cache 0.9.0
File Interpreter ABI Platform Actions
client_query_cache-0.9.0-cp314-abi3-manylinux_2_28_x86_64.whl CPython 3.14 abi3 Linux glibc 2.28+ x86-64 Details for client_query_cache-0.9.0-cp314-abi3-manylinux_2_28_x86_64.whl

Total release size: 529.6 kB

Release files / client_query_cache-0.9.0.tar.gz

Download URL client_query_cache-0.9.0.tar.gz
Size 114.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3c44f1e0369448544743798a80ee6da73dc21e0396a86d72b3b904e8529a40b8
BLAKE2b-256 checksum
How to use checksums
2c049b33919267366b64ad2a24e617c051832aaf30fa1a6d832d02a9a730c562
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 18, 2026.

Transparency log

Release files / client_query_cache-0.9.0-cp314-abi3-manylinux_2_28_x86_64.whl

Download URL client_query_cache-0.9.0-cp314-abi3-manylinux_2_28_x86_64.whl
Size 414.7 kB
Tags CPython 3.14 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
bf10d60757b537d130d15736d6fddf2ef9fdac69eba402c8eea14a1471a4f9bf
BLAKE2b-256 checksum
How to use checksums
2d7766621844e35ad5a758495d4721addc6fa664d9b2a6c57f3ab7e905dbef0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 18, 2026.

Transparency log

Security for client-query-cache 0.9.0

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

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for client_query_cache-0.9.0.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 Oct 18, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page