Skip to main content

Client-side query caching for PyMongo

PyPI Supported Python versions Coverage Typing SLSA Build Level 2

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.

Both cache managers can serve eligible reads from memory, falling back to PyMongo for cache misses and bypasses. MongoDB change streams asynchronously invalidate cached results.

flowchart TB
    App["Python application"]
    API["client-query-cache"]
    Cache["In-memory cache<br/>(per-process or shared across processes)"]
    DB[("MongoDB")]

    App -->|"Query"| API
    API -->|"Cache lookup"| Cache
    Cache -->|"Hit"| API
    API -->|"Miss / bypass"| DB
    DB -.->|"Change-stream 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.14.2

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.14.2
File Size Uploaded Provenance Actions
client_query_cache-0.14.2.tar.gz 154.3 kB With provenance Details for client_query_cache-0.14.2.tar.gz

Built distributions (wheels)

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

Total release size: 1.5 MB

Release files / client_query_cache-0.14.2.tar.gz

Download URL client_query_cache-0.14.2.tar.gz
Size 154.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b189ac652fd782094cbb4dc0f77f5e14933894e1e8c9aee21c208814f5d7cdd0
BLAKE2b-256 checksum
How to use checksums
a0f276ed4983a78a753d9c324a61b9fbfc3ebc589b9d9d972b9ff9176d21b313
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 22, 2026.

Transparency log

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

Download URL client_query_cache-0.14.2-cp314-abi3-manylinux_2_28_x86_64.whl
Size 673.7 kB
Tags CPython 3.14 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
397ef44ef078b7a514e174b06425e8d54576e75568ed4b356263dd6bb0817bd6
BLAKE2b-256 checksum
How to use checksums
e4936a096465e756349d4c3b1611cca559b174413ba7b92ade0ee8a59e118351
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 22, 2026.

Transparency log

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

Download URL client_query_cache-0.14.2-cp314-abi3-manylinux_2_28_aarch64.whl
Size 654.8 kB
Tags CPython 3.14 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
0d76d6cb2b782627930c1b2c23bafee3aaf0ca598c828835082034746496b543
BLAKE2b-256 checksum
How to use checksums
3a154fadb5a733dbb52ca1599ab1f4a53e56c56085b2dd87d115b4740559bbce
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 22, 2026.

Transparency log

Security for client-query-cache 0.14.2

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.14.2.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 22, 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