redis

class litestar.stores.redis.RedisStore[源代码]

基类:NamespacedStore

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

__init__(redis: Redis, namespace: str | None | Literal[_EmptyEnum.EMPTY] = _EmptyEnum.EMPTY, handle_client_shutdown: bool = False) None[源代码]

Initialize RedisStore

参数:
  • redis -- An redis.asyncio.Redis instance

  • namespace -- A key prefix to simulate a namespace in redis. 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 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 | Literal[_EmptyEnum.EMPTY] = _EmptyEnum.EMPTY) RedisStore[源代码]

Initialize a RedisStore instance with a new class:redis.asyncio.Redis instance.

参数:
  • url -- Redis URL to connect to

  • db -- Redis database to use

  • port -- Redis port to use

  • username -- Redis username to use

  • password -- Redis password to use

  • namespace -- Virtual key namespace to use

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, keep_ttl: Literal[False] = False) None[源代码]
async set(key: str, value: str | bytes, expires_in: None = None, *, keep_ttl: Literal[True]) 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

  • keep_ttl -- If True, the TTL of the key will not be changed. If False, the TTL of the key will be set to the value of expires_in

抛出:

ValueError -- If both expires_in and keep_ttl are set, as these options are mutually exclusive

返回:

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.