betty.store module

The key-value store API.

class betty.store.Store

Bases: ABC, Generic

A key-value store.

To test your own subclasses, use betty.test_utils.store.StoreTestBase.

This is thread-safe, which means you can safely use this between different threads.

abstractmethod async get(key: str, /) StoreItem[ItemValueT] | None

Get the item with the given key.

abstractmethod getset(key: str, /) AbstractAsyncContextManager[StoreItemValueSetter[ItemValueT] | StoreItem[ItemValueT]]

Get the item with the given key, or provide a setter to add it within the same atomic operation.

async has(key: str, /) bool

Check if an item with the given key exists.

abstractmethod hasset(key: str, /) AbstractAsyncContextManager[StoreItemValueSetter[ItemValueT] | None]

Check if an item with the given key exists, and if not, provide a setter to add or update it within the same atomic operation.

abstractmethod async set(key: str, value: ItemValueT, *, modified: float | None = None) None

Add or update an item.

abstractmethod with_scope(scope: str, /) Self

Return a new nested store with the given scope.

class betty.store.StoreItem

Bases: ABC, Generic

A store item.

abstract property modified: int | float

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

abstractmethod async value() ItemValueT

Get this item’s value.

class betty.store.TransientStore

Bases: Store, Generic

A key-value store whose items are transient, meaning that may be deleted, and are not guaranteed to persist.

abstractmethod async clear() None

Clear all items from the store.

This operation s unsafe and MAY cause other concurrent operations on this store to fail.

abstractmethod async delete(key: str, /) None

Delete the item with the given key.

This operation s unsafe and MAY cause other concurrent operations on this store to fail.