# Synchronous quick start

This tutorial uses the process-local `CacheManager`. For sharing across workers, see [choose a manager](<https://alessio-locatelli.github.io/client-query-cache/stable/getting-started/choose-a-manager/index.md>) and the [shared FastAPI example](<https://alessio-locatelli.github.io/client-query-cache/stable/examples/fastapi-shared/index.md>).

This program writes a document to your local MongoDB replica set and reads it through a cached view. It uses a dedicated tutorial collection.

```
from pymongo import MongoClient

from client_query_cache import CacheCoreConfig, CacheManager

with (
    MongoClient("mongodb://localhost:27017") as client,
    CacheManager(
        client, cache_config=CacheCoreConfig(enable_telemetry=True)
    ) 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
    )

    first = cached_collection.find_one({"_id": "example"})
    repeated = cached_collection.find_one({"_id": "example"})
    print(first, repeated)
    print("Cache hits:", cache_manager.snapshot().hits)

    items = list(cached_collection.find({"value": 42}))
    print(items)
```

Save the program as `quick_start.py` and run `python quick_start.py` in the environment where you installed the library. On an eligible deployment with a healthy stream, repeated reads can hit the cache. If caching is unavailable, both reads execute through PyMongo; [monitoring](<https://alessio-locatelli.github.io/client-query-cache/stable/operations/monitoring/index.md>) explains how to inspect bypasses. Startup and concurrent changes can affect the observed hit count.

Keep the PyMongo `collection` for writes and administration. The cached view exposes [six read methods](<https://alessio-locatelli.github.io/client-query-cache/stable/usage/cached-reads/index.md>); `find` and `aggregate` return cursors. Iterate them or use `list(cursor)` / `cursor.to_list()` to materialize their documents. Use `.raw` when a read must execute against MongoDB.

The context managers close the cache manager before the client. A manager never closes your client, and closing only the client leaves the manager's background task running. Reuse a long-lived manager across application requests; see [deployment](<https://alessio-locatelli.github.io/client-query-cache/stable/operations/deployment/index.md#capacity-estimation>).

A cached read after a write can still see an earlier value until invalidation arrives. Use a direct PyMongo read where freshness is required, with the appropriate session and concerns; see [consistency](<https://alessio-locatelli.github.io/client-query-cache/stable/usage/consistency/index.md>). Eligible cache misses read from the primary with `majority` read concern even though this program leaves read concern unspecified, so they can differ from a direct read that uses the deployment's default, normally `local`; see [bypass conditions](<https://alessio-locatelli.github.io/client-query-cache/stable/reference/api/index.md#bypass-conditions>).
