ahvn.cache.base module¶
Abstract cache protocol and common helpers.
Implements the CacheEntry structure and BaseCache with memoization decorators for sync/async functions and streaming generators.
- class ahvn.cache.base.CacheEntry(func, inputs=<factory>, output=Ellipsis, expected=Ellipsis, metadata=<factory>, _key=None)[源代码]¶
基类:
objectA universal object to store cache entries, containing the function (name), inputs, output, and optional metadata.
- 参数:
- property key¶
- property value¶
- classmethod from_args(func='', output=..., expected=..., metadata=None, exclude=None, **inputs)[源代码]¶
Creates a CacheEntry from function arguments.
- 参数:
func (Union[Callable, str]) -- The function or its name to be cached.
output (Any) -- The output of the function.
expected (Any) -- The expected output of the function.
metadata (Optional[Dict[str, Any]]) -- Optional metadata to store with the cache entry
exclude (Optional[Iterable[str]]) -- Keys to exclude from inputs.
**inputs (
Any) -- Arbitrary keyword arguments representing the inputs to the function.
- 返回类型:
- clone(**updates)[源代码]¶
Creates a clone of the CacheEntry with optional updates to its attributes.
- 参数:
**updates -- Arbitrary keyword arguments to update the CacheEntry attributes.
- 返回:
A new CacheEntry instance with updated attributes.
- 返回类型:
- annotate(expected=..., metadata=None)[源代码]¶
Annotates the CacheEntry with expected output and metadata.
- 参数:
- 返回:
A new CacheEntry instance with the annotation.
- 返回类型:
- class ahvn.cache.base.BaseCache(exclude=None, *args, **kwargs)[源代码]¶
基类:
ABCAn abstract base class for cache implementations.
The class provides memoize and batch_memoize decorators to cache function results. The class requires subclasses to implement storage and retrieval methods. The caching supports both synchronous and asynchronous functions.
- _cache¶
A dictionary to store cache entries.
- Type:
Dict[str, CacheEntry]
- Abstract Methods:
_get(key, default): Retrieves a dict (CacheEntry) from the cache by its key. Use Ellipsis as default to raise KeyError if not found (to avoid collisions with functions returning None). _set(key, value): Sets a cache entry in the cache. _remove(key): Removes a cache entry from the cache by its key. _itervalues(): Returns an iterator over the values in the cache. _clear(): Clears the cache.
- Optional Override Methods:
_has(key): Determines if a cache entry exists for the given key. __len__(): Returns the number of entries in the cache.
备注
The default implementations of __len__, _has, and batch operations are not optimized and may be slow for large stores. Subclasses are encouraged to override these for efficiency.
It is worth noticing that only the __getitem__ (implemented by _get) and add (implemented by _set) are used during memoize.
- __init__(exclude=None, *args, **kwargs)[源代码]¶
Initialization.
- 参数:
exclude (Optional[Iterable[str]]) -- Keys to exclude from inputs when creating cache entries.
*args -- Additional positional arguments.
**kwargs -- Additional keyword arguments.
- set_exclude(exclude=None)[源代码]¶
Set the keys to exclude from inputs when creating cache entries.
- 参数:
exclude (Optional[Iterable[str]]) -- Keys to exclude from inputs when creating cache entries.
- add_exclude(exclude=None)[源代码]¶
Add keys to exclude from inputs when creating cache entries.
- 参数:
exclude (Optional[Iterable[str]]) -- Keys to add to the exclusion list.
- __contains__(key)[源代码]¶
Checks if a cache entry exists for the given key.
- 参数:
key (Union[int, CacheEntry]) -- The key or CacheEntry to check in the cache.
- 返回:
True if the cache entry exists, False otherwise.
- 返回类型:
- __getitem__(key)[源代码]¶
Retrieves a cache entry for the given function and inputs.
- 参数:
key (Union[int, str, CacheEntry]) -- The key or CacheEntry to retrieve.
- 返回:
The cached entry if found. Otherwise Ellipsis.
- 返回类型:
- __setitem__(key, value)[源代码]¶
Sets a cache entry for the given function and inputs.
- 参数:
key (Union[int, str, CacheEntry]) -- The key or CacheEntry to set.
value (Union[Dict[str, Any], CacheEntry]) -- The value to cache.
- set(func, output=..., expected=..., metadata=None, **kwargs)[源代码]¶
Sets a cached value for the given function and inputs.
- 参数:
func (Union[Callable, str]) -- The function or its name to cache the value for.
output (Any) -- The output to cache.
expected (Any) -- The expected output of the function.
metadata (Optional[Dict[str, Any]]) -- Optional metadata to store with the cache entry.
**kwargs -- Arbitrary keyword arguments representing the inputs to the function.
- add(entry)[源代码]¶
Sets a cache entry by directly adding it to the cache.
- 参数:
entry (CacheEntry) -- The cache entry to add.
- annotate(func, expected=..., metadata=None, **kwargs)[源代码]¶
Annotates a cached value for the given function and inputs.
- 参数:
func (Union[Callable, str]) -- The function or its name to annotate the cached value for.
expected (Any) -- The expected output of the function. If the cache entry exists, its expected will be modified, but its output will remain unchanged. Otherwise, a new entry with output and expected both set as the annotation will be created. If set to ... and the cache entry exists, it will use the actual output as the annotation. If set to ... but the cache entry does not exist, raise an error.
metadata (Optional[Dict[str, Any]]) -- Optional metadata to store with the cache entry.
**kwargs -- Arbitrary keyword arguments representing the inputs to the function.
- 抛出:
ValueError -- If the original cache entry does not exist and expected is not provided.
- __delitem__(key)[源代码]¶
Deletes a cache entry for the given function and inputs.
- 参数:
key (Union[int, str, CacheEntry]) -- The key or CacheEntry of the cache entry to delete.
- unset(func, **kwargs)[源代码]¶
Deletes a cache entry for the given function and inputs.
- 参数:
func (Union[Callable, str]) -- The function or its name to delete the cache entry for.
**kwargs -- Arbitrary keyword arguments representing the inputs to the function.
- remove(entry)[源代码]¶
Deletes a cache entry by directly removing it from the cache.
- 参数:
entry (CacheEntry) -- The cache entry to remove.
- __iter__()[源代码]¶
Iterates over the cache entries.
- 生成器:
CacheEntry -- The CacheEntry objects in the cache.
- 返回类型:
- pop()[源代码]¶
Pops an arbitrary cache entry from the cache.
- 返回:
The popped CacheEntry if the cache is not empty, otherwise None.
- 返回类型:
Optional[CacheEntry]
- popall()[源代码]¶
Pops all cache entries from the cache.
- 返回:
A list of all popped CacheEntry objects.
- 返回类型:
List[CacheEntry]
- memoize(func=None, *, name=None)[源代码]¶
Decorator (or decorator factory) to cache the output of a function based on its inputs.
- Usage:
@cache.memoize def f(...): ...
@cache.memoize(name="xxx") def g(...): ...
When name is provided, it is used as the function identifier in the cache key. Otherwise, the wrapped function's name is used.
- batch_memoize(func=None, *, name=None)[源代码]¶
Decorator (or decorator factory) to cache the output of a function based on its inputs in batch mode.
- Usage:
@cache.batch_memoize def f(...): ...
@cache.batch_memoize(name="xxx") def g(...): ...
- 参数:
func (Callable) -- The function to cache.
name (Optional[str]) -- Optional name for the function in the cache key.
- 返回:
The wrapped function with batch caching.
- 返回类型:
Callable