Skip to main content

Caches

A registry of named caches under one base directory.

Usage

Source

Caches(
    base_dir,
    *,
    on_open_error=None,
    retry_open_after=10.0,
    _monotonic=time.monotonic,
    **defaults
)

Caches[Literal['general', 'show_pages']] makes a misspelled name a type error.

Parameters

base_dir: str | os.PathLike[str]

Where the caches live: each one in base_dir/<name> unless register says otherwise. Created when the first cache opens.

on_open_error: Callable[[str, Exception], object] | None = None

Called as on_open_error(name, exc) when a cache can’t open (default: log at ERROR on the diskcache_oxide logger, or at WARNING with no traceback for a Timeout). Whatever it raises is logged.

retry_open_after: float = 10.0

Seconds before a cache that failed to open is tried again (default 10). Lookups in between get a NullCache without trying. A Timeout means busy rather than broken, so that’s tried again after at most 1 s.

**defaults: Any
Cache settings for every cache, such as timeout=1.0, durable=True or on_decode_error='miss'. register can override them. A callable on_decode_error here is called as (name, key, exc), so one reporter can tell the caches apart.

Examples

from diskcache_oxide import Caches

caches = Caches('/var/cache/my-app', timeout=1.0, on_decode_error='miss')
caches.register('general')
caches.register('pages', size_limit=2 * 2**30)
caches.register('rate_limits', shards=8)

caches['general'].set('greeting', 'hello')
caches.fanout('rate_limits').incr('ip:203.0.113.7')
caches.stats()['pages'].count