Skip to main content

Client-side query caching for PyMongo

PyPI Supported Python versions 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.

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.

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

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.1
File Size Uploaded Provenance Actions
client_query_cache-0.9.1.tar.gz 121.5 kB With provenance Details for client_query_cache-0.9.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for client-query-cache 0.9.1
File Interpreter ABI Platform Actions
client_query_cache-0.9.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.9.1-cp314-abi3-manylinux_2_28_x86_64.whl

Total release size: 539.7 kB

Release files / client_query_cache-0.9.1.tar.gz

Download URL client_query_cache-0.9.1.tar.gz
Size 121.5 kB
Tags Source
SHA-256 checksum
How to use checksums
0e55c906df9605967a67c3f00e6f993e4c702654a06055cab82993eb2caf4f47
BLAKE2b-256 checksum
How to use checksums
7c4410104c92aa1066c59fa99e2439fb3733f9f4ce1e5a6ab17325883a358a97
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 24, 2026.

Transparency log

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

Download URL client_query_cache-0.9.1-cp314-abi3-manylinux_2_28_x86_64.whl
Size 418.2 kB
Tags CPython 3.14 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
a31141962f903f659744111e307340271f428d1dab1fb1c1b847b2bceca47b27
BLAKE2b-256 checksum
How to use checksums
04468c179b8c6b37773cf771c7d5bc657810faafaab53f75fcd6c8c763a27440
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 24, 2026.

Transparency log

Security for client-query-cache 0.9.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

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

Select from 2 files

Attestation for client_query_cache-0.9.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 Oct 24, 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