risa.store.memory¶
MemoryStore, the store that keeps state in this process.
DEFAULT_MAX_ENTRIES
module-attribute
¶
How many entries a MemoryStore keeps before it evicts the least recently used.
MemoryStore
¶
MemoryStore(
*,
max_entries: int = DEFAULT_MAX_ENTRIES,
clock: Callable[[], float] = monotonic,
)
Bases: Store
A store that keeps state in this process's memory.
Meant for development and tests: the state is lost on every restart, and other
processes cannot see it, so the client warns when an InStore view uses one. Past
max_entries, each new entry pushes out the least recently used one.
Parameters:
-
max_entries(int, default:DEFAULT_MAX_ENTRIES) –How many entries to keep.
-
clock(Callable[[], float], default:monotonic) –The monotonic clock, in seconds, that expiry is measured on.
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: