Decorators¶
@cached
performs memoization of a wrapped function:
from cachetory.caches.sync import Cache
from cachetory.decorators.sync import cached
cache = Cache[int, ...](backend=..., serializer=...)
@cached(cache)
def expensive_function(x: int) -> int:
return 42 * x
Key functions¶
There are a few make_key
functions provided by default:
cachetory.decorators.shared.make_default_key ¶
Generate a human-readable cache key out of decorated function fully-qualified name and stringified arguments.
The length of the key depends on the arguments.
Source code in cachetory/decorators/shared.py
cachetory.decorators.shared.make_default_hashed_key ¶
Generate a hashed fixed-length cache key given the callable and the arguments it's being called with.
Uses blake2s
as the fastest algorithm from hashlib
.
Source code in cachetory/decorators/shared.py
Purging cache¶
Specific cached value can be deleted using the added purge()
function, which accepts the same arguments as the original wrapped callable:
Synchronous @cached
¶
Apply memoization to the wrapped callable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cache
|
Cache[ValueT, WireT] | Callable[..., Cache[ValueT, WireT] | None] | None
|
|
required |
make_key
|
Callable[..., str]
|
callable to generate a custom cache key per each call. |
make_default_key
|
time_to_live
|
timedelta | Callable[..., timedelta | None] | None
|
cached value expiration time or callable that returns the expiration time. The callable needs to accept keyword arguments, and it is given the cache key to compute the expiration time. |
None
|
if_not_exists
|
bool
|
controls concurrent sets: if |
False
|
exclude
|
Callable[[str, ValueT], bool] | None
|
Optional callable to prevent a key-value pair from being cached if the callable returns true. |
None
|
Source code in cachetory/decorators/sync.py
Cached callable protocol¶
Protocol of the wrapped callable.
Source code in cachetory/decorators/sync.py
purge ¶
Delete the value that was cached using the same call arguments.
Returns:
Type | Description |
---|---|
bool
|
whether a cached value existed |
Asynchronous @cached
¶
Apply memoization to the wrapped callable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cache
|
Cache[ValueT, WireT] | Callable[..., Cache[ValueT, WireT] | None] | Callable[..., Awaitable[Cache[ValueT, WireT] | None]] | None
|
|
required |
make_key
|
Callable[..., str]
|
callable to generate a custom cache key per each call. |
make_default_key
|
time_to_live
|
timedelta | Callable[..., timedelta | None] | Callable[..., Awaitable[timedelta]] | None
|
cached value expiration time or a callable (sync or async) that returns the expiration time. The callable needs to accept keyword arguments, and it is given the cache key to compute the expiration time. |
None
|
if_not_exists
|
bool
|
controls concurrent sets: if |
False
|
exclude
|
Callable[[str, ValueT], bool] | Callable[[str, ValueT], Awaitable[bool]] | None
|
Optional callable to prevent a key-value pair from being cached if the callable returns true. |
None
|
Source code in cachetory/decorators/async_.py
Cached callable protocol¶
Protocol of the wrapped callable.
Source code in cachetory/decorators/async_.py
purge
async
¶
Delete the value that was cached using the same call arguments.
Returns:
Type | Description |
---|---|
bool
|
whether a cached value existed |