anystore.decorators
Decorate functions to store results in a configurable cache and retrieve cached results on next call.
Example
The cache key is computed based on the input arguments, but can be configured.
See below for reference details.
anycache(func=None, store=None, model=None, key_func=None, serialization_mode='auto', serialization_func=None, deserialization_func=None, ttl=None, **store_kwargs)
Cache a function call in a configurable cache backend. By default, the default store is used (configured via environment)
Example
Note
If the key_func
returns None
as the computed cache key, the result
will not be cached (this can be used to dynamically disable caching
based on function input)
See anystore.serialize
for serialization reference.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable[..., Any] | None
|
The function to wrap |
None
|
serialization_mode
|
Mode | None
|
"auto", "pickle", "json", "raw" |
'auto'
|
serialization_func
|
Callable | None
|
Function to use to serialize |
None
|
deserialization_func
|
Callable | None
|
Function to use to deserialize, takes bytes as input |
None
|
model
|
BaseModel | None
|
Pydantic model to use for serialization from a json bytes string |
None
|
key_func
|
Callable[..., str | None] | None
|
Function to compute the cache key |
None
|
ttl
|
int | None
|
Key ttl for supported backends |
None
|
**store_kwargs
|
Any
|
Any other store options or backend specific configuration to pass through |
{}
|
Returns:
Name | Type | Description |
---|---|---|
Callable |
Callable[..., Any]
|
The decorated function |
Source code in anystore/decorators.py
async_anycache(func=None, **store_kwargs)
Async implementation of the @anycache decorator