NestedCache: Simple, Recursive In-Memory Caching#
- class my.caches.NestedCache.NestedCache(*, signature: tuple[type, ...], children: dict[Hashable, NestedCache] = {}, data: dict[Any, Value] = {}, size: int = 0, max_size: Annotated[int, Gt(gt=0)] = 4096, bucket_size: Annotated[int, Gt(gt=0)] = 256)#
Multi-level hierarchical cache with automatic pruning.
Supports arbitrary nesting depth determined by the signature tuple length. Each level maintains LRU ordering. Pruning is distributed proportionally across child caches based on their sizes.
Examples
Store and retrieve values under two-level key paths:
>>> from my import NestedCache >>> cache = NestedCache(signature=(str, int)) >>> cache[('user', 1)] = 'robb' >>> cache.set(('user', 2), 'ada') 1 >>> cache[('user', 1)] 'robb' >>> len(cache) 2 >>> cache.delete(('user', 2)) 1
- property NestedCache.depth: int#
The number of key levels in this cache (i.e. the length of its signature).
- NestedCache.set(keys: list | tuple, value: Value) int#
Set a value at the specified key path.
- Parameters:
keys – Path through nested levels (length must match depth).
value – Value to store.
- Returns:
Number of new items added (0 if key existed, 1 if new).
- Raises:
ValueError – If keys length doesn’t match cache depth.