betty.cache package

Submodules

Module contents

Provide the Cache API.

class betty.cache.Cache[source]

Bases: Generic[_CacheItemValueContraT], ABC

Provide a cache.

Implementations MUST be thread-safe.

abstract async clear() None[source]

Clear all items from the cache.

Return type:

None

abstract async delete(cache_item_id: str) None[source]

Delete the cache item with the given ID.

Parameters:

cache_item_id (str)

Return type:

None

abstract get(cache_item_id: str) AsyncContextManager[betty.cache.CacheItem[betty.cache._CacheItemValueContraT] | None][source]

Get the cache item with the given ID.

Parameters:

cache_item_id (str)

Return type:

typing.AsyncContextManager[typing.Optional[betty.cache.CacheItem[typing.TypeVar(_CacheItemValueContraT, contravariant=True)]]]

abstract getset(cache_item_id: str, *, wait: bool = True) AsyncContextManager[tuple[betty.cache.CacheItem[betty.cache._CacheItemValueContraT] | None, collections.abc.Callable[[betty.cache._CacheItemValueContraT], collections.abc.Awaitable[None]] | None]][source]

Get the cache item with the given ID, and provide a setter to add or update it within the same atomic operation.

If wait is False and no lock can be acquired, return None, None. Otherwise return: 0. A cache item if one could be found, or else None. 1. An asynchronous setter that takes the cache item’s value as its only argument.

Parameters:
Return type:

typing.AsyncContextManager[tuple[typing.Optional[betty.cache.CacheItem[typing.TypeVar(_CacheItemValueContraT, contravariant=True)]], collections.abc.Callable[[typing.TypeVar(_CacheItemValueContraT, contravariant=True)], collections.abc.Awaitable[None]] | None]]

abstract async set(cache_item_id: str, value: betty.cache._CacheItemValueContraT, *, modified: int | float | None = None) None[source]

Add or update a cache item.

Parameters:
Return type:

None

abstract with_scope(scope: str) Self[source]

Return a new nested cache with the given scope.

Parameters:

scope (str)

Return type:

typing.Self

class betty.cache.CacheItem[source]

Bases: Generic[_CacheItemValueCoT], ABC

A cache item.

abstract property modified: int | float

Get the time this cache item was last modified, in seconds.

abstract async value() betty.cache._CacheItemValueCoT[source]

Get this cache item’s value.

Return type:

typing.TypeVar(_CacheItemValueCoT, covariant=True)