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 read latency on a logarithmic scale: direct local MongoDB read, 103 microseconds; direct Atlas M0 read, 65.5 milliseconds; cached read in either deployment, 8.01 microseconds. Illustrative read latency on a logarithmic scale: direct local MongoDB read, 103 microseconds; direct Atlas M0 read, 65.5 milliseconds; cached read in either deployment, 8.01 microseconds.

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.13.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.13.0
File Size Uploaded Provenance Actions
client_query_cache-0.13.0.tar.gz 166.5 kB With provenance Details for client_query_cache-0.13.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for client-query-cache 0.13.0
File Interpreter ABI Platform Actions
client_query_cache-0.13.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.13.0-cp314-abi3-manylinux_2_28_x86_64.whl
client_query_cache-0.13.0-cp314-abi3-manylinux_2_28_aarch64.whl CPython 3.14 abi3 Linux glibc 2.28+ ARM64 Details for client_query_cache-0.13.0-cp314-abi3-manylinux_2_28_aarch64.whl

Total release size: 1.5 MB

Release files / client_query_cache-0.13.0.tar.gz

Download URL client_query_cache-0.13.0.tar.gz
Size 166.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f26af975e57b3a990fad13806a96e221abfd66ade1d0d131b194836172563868
BLAKE2b-256 checksum
How to use checksums
c7630354c72d47f01ab7f08c3468f476c2204fac5de2db5d1aa4d49ad2f775e9
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 6, 2026.

Transparency log

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

Download URL client_query_cache-0.13.0-cp314-abi3-manylinux_2_28_x86_64.whl
Size 697.3 kB
Tags CPython 3.14 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
16f99ad5957f9aca1e4fba5119b4a940a1b37a9aba9357594677feec465d2184
BLAKE2b-256 checksum
How to use checksums
fc31415216c0e7c003f6ae7cc71da6b79cd753eed1db3ac9aeb0c4d2984f2534
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 6, 2026.

Transparency log

Release files / client_query_cache-0.13.0-cp314-abi3-manylinux_2_28_aarch64.whl

Download URL client_query_cache-0.13.0-cp314-abi3-manylinux_2_28_aarch64.whl
Size 668.3 kB
Tags CPython 3.14 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
bf86319a6e01b00167e7388076317196083341c1dee3a722d66c4e14a7db6b25
BLAKE2b-256 checksum
How to use checksums
7b6f3e9d0838ec1da4585a79985bf60d4465503b48a7e59cd158f8501030f813
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 6, 2026.

Transparency log

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

All 3 files in this release have been uploaded with provenance metadata.

Select from 3 files

Attestation for client_query_cache-0.13.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 Nov 6, 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