valkey

class litestar.stores.valkey.ValkeyStore[原始碼]

基底類別:NamespacedStore

Valkey based, thread and process safe asynchronous key/value store.

__init__(valkey: Valkey, namespace: str | None | Literal[_EmptyEnum.EMPTY] = _EmptyEnum.EMPTY, handle_client_shutdown: bool = False) None[原始碼]

Initialize ValkeyStore

參數:
  • valkey -- An valkey.asyncio.Valkey instance

  • namespace -- A key prefix to simulate a namespace in valkey. If not given, defaults to LITESTAR. Namespacing can be explicitly disabled by passing None. This will make delete_all() unavailable.

  • handle_client_shutdown -- If True, handle the shutdown of the valkey instance automatically during the store's lifespan. Should be set to True unless the shutdown is handled externally

classmethod with_client(url: str = 'valkey://localhost:6379', *, db: int | None = None, port: int | None = None, username: str | None = None, password: str | None = None, namespace: str | None | Literal[_EmptyEnum.EMPTY] = _EmptyEnum.EMPTY) ValkeyStore[原始碼]

Initialize a ValkeyStore instance with a new class:valkey.asyncio.Valkey instance.

參數:
  • url -- Valkey URL to connect to

  • db -- Valkey database to use

  • port -- Valkey port to use

  • username -- Valkey username to use

  • password -- Valkey password to use

  • namespace -- Virtual key namespace to use

with_namespace(namespace: str) ValkeyStore[原始碼]

Return a new ValkeyStore with a nested virtual key namespace. The current instances namespace will serve as a prefix for the namespace, so it can be considered the parent namespace.

async set(key: str, value: str | bytes, expires_in: int | timedelta | None = None) None[原始碼]

Set a value.

參數:
  • key -- Key to associate the value with

  • value -- Value to store

  • expires_in -- Time in seconds before the key is considered expired

回傳:

None

async get(key: str, renew_for: int | timedelta | None = None) bytes | None[原始碼]

Get a value.

參數:
  • key -- Key associated with the value

  • renew_for -- If given and the value had an initial expiry time set, renew the expiry time for renew_for seconds. If the value has not been set with an expiry time this is a no-op. Atomicity of this step is guaranteed by using a lua script to execute fetch and renewal. If renew_for is not given, the script will be bypassed so no overhead will occur

回傳:

The value associated with key if it exists and is not expired, else None

async delete(key: str) None[原始碼]

Delete a value.

If no such key exists, this is a no-op.

參數:

key -- Key of the value to delete

async delete_all() None[原始碼]

Delete all stored values in the virtual key namespace.

引發:

ImproperlyConfiguredException -- If no namespace was configured

async exists(key: str) bool[原始碼]

Check if a given key exists.

async expires_in(key: str) int | None[原始碼]

Get the time in seconds key expires in. If no such key exists or no expiry time was set, return None.