risa.store.protocol¶
The contract a state store implements.
DEFAULT_LOCK_TIMEOUT
module-attribute
¶
Seconds Store.lock() waits for a key before it raises LockTimeoutError.
Store
¶
Bases: Protocol
Where risa keeps the state of InStore views.
Implement it to keep state somewhere risa ships no store for, and check the
implementation with risa.store.testing.verify_store. A missing or expired entry is
never an error: it reads as None. Apart from LockTimeoutError from lock, any
exception a method raises means the backend could not be reached, and risa reports it
as StoreUnavailableError.
delete
async
¶
delete(key: str) -> None
Remove the entry under key, if there is one.
Parameters:
-
key(str) –The entry's key, as for
get_versioned.
get_versioned
async
¶
get_versioned(key: str) -> VersionedEntry | None
Return the data and version under key, or None if there is none or it has expired.
Parameters:
-
key(str) –The entry's key, an opaque string. risa's are
{prefix}:1:{hex}, and it passes the one key of a view's record to every method,lockincluded.
Returns:
-
VersionedEntry | None–The bytes last written with their version, or
None.
lock
¶
lock(
key: str, *, timeout: float = DEFAULT_LOCK_TIMEOUT
) -> AbstractAsyncContextManager[None]
Hold key exclusively while the returned context manager is entered.
risa holds it for the length of a click, so clicks on one view run one after
another. A store that several processes share should exclude all of them; when such
a lock lapses, the version check of put_if_version still decides whose write
lands.
Parameters:
-
key(str) –The key to hold; risa passes the key of the record it is about to read and write. Holding one key does not make another wait, and the key needs no entry.
-
timeout(float, default:DEFAULT_LOCK_TIMEOUT) –Seconds to wait for the lock, fractions included, before raising
LockTimeoutError.
Raises:
-
LockTimeoutError–If the lock is not granted within
timeoutseconds.
put
async
¶
Write value under key, whether or not an entry exists.
The entry's version changes. ttl is the entry's lifetime in seconds from now, and
None clears any expiry, so the entry lives until it is deleted.
Parameters:
-
key(str) –The entry's key, as for
get_versioned. -
value(bytes) –The bytes to keep.
get_versionedreturns them unchanged, whatever they hold, empty included. -
ttl(float | None) –Seconds the entry lives, counted from this write and fractions included.
Nonegives it no expiry, which also clears the expiry an earlier write set.
put_if_version
async
¶
Write value under key only if the entry is still at version expected.
The compare and the write are one atomic step: of several writers expecting the same
version, exactly one succeeds. ttl works as in put.
Parameters:
-
key(str) –The entry's key, as for
get_versioned. -
value(bytes) –The bytes to keep if the write goes through.
-
expected(int) –A version from
get_versioned, or returned by an earlierput_if_version. The write goes through only while the entry is still at exactly that version. Versions compare only within one entry's life: a store may restart them once the entry is deleted or has expired. -
ttl(float | None) –Seconds the entry lives from this write, as in
put.Nonegives it no expiry.
Returns:
-
int | None–The entry's new version, or
Noneif the entry is missing, expired, or at a different version, in which case nothing was written.
touch
async
¶
Restart the lifetime of the entry under key, which now expires ttl seconds from now.
The data and version stay as they are, and a missing key stays missing.
Parameters: