betty.cache package¶
Submodules¶
Module contents¶
Provide the Cache API.
- class betty.cache.Cache[source]¶
Bases:
Generic
[_CacheItemValueContraT
],ABC
A cache.
Implementations SHOULD be multiprocessing-safe. It is deprecated for implementations not to be multiprocessing-safe as of Betty 0.4.10. As of Betty 0.5.0, implementations MUST be multiprocessing-safe.
To test your own subclasses, use
betty.test_utils.cache.CacheTestBase
.This is thread-safe, which means you can safely use this between different threads.
- abstractmethod async delete(cache_item_id: str) None [source]¶
Delete the cache item with the given ID.
- abstractmethod get(cache_item_id: str) AsyncContextManager[CacheItem[_CacheItemValueContraT] | None, bool | None] [source]¶
Get the cache item with the given ID.
- abstractmethod getset(cache_item_id: str) AsyncContextManager[tuple[CacheItem[_CacheItemValueContraT] | None, Callable[[_CacheItemValueContraT], Awaitable[None]]], bool | None] [source]¶
- abstractmethod getset(cache_item_id: str, *, wait: Literal[False] = False) AsyncContextManager[tuple[CacheItem[_CacheItemValueContraT] | None, Callable[[_CacheItemValueContraT], Awaitable[None]] | None], bool | None]
Get the cache item with the given ID, and provide a setter to add or update it within the same atomic operation.
If
wait
isFalse
and no lock can be acquired, returnNone, None
. Otherwise return: 0. A cache item if one could be found, or elseNone
. 1. An asynchronous setter that takes the cache item’s value as its only argument.