Skip to content

Cache with Redis or disk

The CacheLayer store is a small cashews-shaped protocol (get / set / delete / delete_match), so a cashews.Cache plugs in with no adapter. Use Redis for a cache shared across processes, or a disk backend for one that survives restarts.

"""Cache with Redis (or disk) through cashews.

CacheLayer's store is a small cashews-shaped protocol (get / set / delete /
delete_match), so a cashews.Cache plugs in directly. Point it at Redis for a
cache shared across processes, or at a disk backend for one that survives
restarts. Keys are namespaced, so environments never collide in a shared store.
"""

from __future__ import annotations

from cashews import Cache

from storix import CacheLayer, cache, get_storage


store = Cache()
store.setup('redis://localhost:6379')  # or 'disk://.cache' for a local disk cache

fs = get_storage('azure').with_layer(
    CacheLayer,
    store=store,  # a cashews.Cache satisfies the CacheStore protocol as-is
    du=cache(ttl=60),
    read=cache(max_bytes=8 << 20),
    environment='prod',
)

The store is the only thing that changes; the per-operation options (metadata / du / read / url, each True or a cache(...) spec) work the same over any store. See Layers for the full set.

Single writer

A cache cannot see writes made outside the layer (another process, the cloud console). Every write through the layer evicts what it touched, so your own session stays consistent, but pass a ttl to bound staleness whenever a store is shared with writers storix does not control.