redis#
- class litestar.stores.redis.RedisStore#
Bases:
NamespacedStore
Redis based, thread and process safe asynchronous key/value store.
- __init__(redis: ~redis.asyncio.client.Redis, namespace: str | None | ~typing.Literal[<_EmptyEnum.EMPTY: 0>] = _EmptyEnum.EMPTY, handle_client_shutdown: bool = False) None #
Initialize
RedisStore
- Parameters:
redis¶ – An
redis.asyncio.Redis
instancenamespace¶ – A key prefix to simulate a namespace in redis. If not given, defaults to
LITESTAR
. Namespacing can be explicitly disabled by passingNone
. This will makedelete_all()
unavailable.handle_client_shutdown¶ – If
True
, handle the shutdown of the redis instance automatically during the store’s lifespan. Should be set to True unless the shutdown is handled externally
- classmethod with_client(url: str = 'redis://localhost:6379', *, db: int | None = None, port: int | None = None, username: str | None = None, password: str | None = None, namespace: str | None | ~typing.Literal[<_EmptyEnum.EMPTY: 0>] = _EmptyEnum.EMPTY) RedisStore #
Initialize a
RedisStore
instance with a new class:redis.asyncio.Redis instance.
- with_namespace(namespace: str) RedisStore #
Return a new
RedisStore
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.
- async get(key: str, renew_for: int | timedelta | None = None) bytes | None #
Get a value.
- Parameters:
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. Ifrenew_for
is not given, the script will be bypassed so no overhead will occur
- Returns:
The value associated with
key
if it exists and is not expired, elseNone
- async delete(key: str) None #
Delete a value.
If no such key exists, this is a no-op.
- Parameters:
key¶ – Key of the value to delete
- async delete_all() None #
Delete all stored values in the virtual key namespace.
- Raises:
ImproperlyConfiguredException – If no namespace was configured