ext.cache¶
cache
¶
Store-level caching middleware with TTL-based expiration.
Wraps a Store in a proxy that caches read-only operations (existence checks, metadata, listings, content) and automatically invalidates on mutations.
Example
CacheStats
dataclass
¶
Snapshot of cache hit/miss statistics.
CacheBackend
¶
Bases: Protocol
Protocol for pluggable cache backends.
Implement this protocol to provide a custom cache backend to
cache(store, cache_backend=my_backend). The default implementation
is MemoryCache.
MemoryCache
¶
Thread-safe in-memory cache backend with lazy TTL eviction.
Entries are stored as {key: (value, expiry)} where expiry is a
time.monotonic() deadline.
When max_entries is set, the cache evicts the least-recently-used
entry when the limit is exceeded (LRU eviction). Without a bound,
metadata entries (exists, is_file, listings) for many distinct
paths can grow without limit during the TTL window.
CachedStore
¶
CachedStore(
inner: Store,
*,
ttl: float,
max_content_size: int | None,
max_listing_size: int | None,
max_entries: int | None,
cache_backend: CacheBackend | None,
_prefix: str = "",
)
Bases: ProxyStore
Proxy Store that caches read operations with TTL-based expiration.
All Store methods are delegated to the inner store. Read-only
methods use the cache; mutating methods invalidate affected entries.
Only methods with additional behavior (invalidate, clear_cache,
ping, close, child) are documented individually below.
Do not construct directly -- use cache().
invalidate
¶
Remove all cached entries for path and its ancestor directories.
cache
¶
cache(
store: Store,
*,
ttl: float = 300.0,
max_content_size: int | None = None,
max_listing_size: int | None = None,
max_entries: int | None = None,
cache_backend: CacheBackend | None = None,
) -> CachedStore
Wrap a Store with read-through caching.
Parameters:
-
store(Store) –The Store to wrap.
-
ttl(float, default:300.0) –Time-to-live in seconds for cache entries (default 300).
-
max_content_size(int | None, default:None) –Maximum byte length for
read_bytescaching. Files larger than this are returned without caching.Nonemeans unlimited. -
max_listing_size(int | None, default:None) –Maximum number of items in a listing result (
iter_children,list_files,list_folders,glob) for caching. Listings with more items than this are returned without caching.Nonemeans unlimited. -
max_entries(int | None, default:None) –Maximum number of cache entries. When exceeded, the least-recently-used entry is evicted.
Nonemeans no limit. Ignored when cache_backend is provided. -
cache_backend(CacheBackend | None, default:None) –Optional custom cache. When
None, aMemoryCacheis created.
Returns:
-
CachedStore–A
CachedStoreproxy.
Raises:
-
ValueError–If ttl, max_content_size, or max_listing_size is not positive when set.
See also¶
- Cache — guide to Store-level caching with TTL
- Caching example — caching middleware in action