redis

class litestar.channels.backends.redis.RedisChannelsBackend[source]

Bases: ChannelsBackend, ABC

__init__(*, redis: Redis, key_prefix: str, stream_sleep_no_subscriptions: int) None[source]

Base redis channels backend.

Parameters:
  • redis – A redis.asyncio.Redis instance

  • key_prefix – Key prefix to use for storing data in redis

  • stream_sleep_no_subscriptions – Amount of time in milliseconds to pause the stream_events() generator, should no subscribers exist

class litestar.channels.backends.redis.RedisChannelsPubSubBackend[source]

Bases: RedisChannelsBackend

__init__(*, redis: Redis, stream_sleep_no_subscriptions: int = 1, key_prefix: str = 'LITESTAR_CHANNELS') None[source]

Redis channels backend, Pub/Sub.

This backend provides low overhead and resource usage but no support for history.

Parameters:
  • redis – A redis.asyncio.Redis instance

  • key_prefix – Key prefix to use for storing data in redis

  • stream_sleep_no_subscriptions – Amount of time in milliseconds to pause the stream_events() generator, should no subscribers exist

async on_startup() None[source]

Called by the plugin on application startup

async on_shutdown() None[source]

Called by the plugin on application shutdown

async subscribe(channels: Iterable[str]) None[source]

Subscribe to channels, and enable publishing to them

async unsubscribe(channels: Iterable[str]) None[source]

Stop listening for events on channels

async publish(data: bytes, channels: Iterable[str]) None[source]

Publish data to channels

Note

This operation is performed atomically, using a lua script

async stream_events() AsyncGenerator[tuple[str, Any], None][source]

Return a generator, iterating over events of subscribed channels as they become available.

If no channels have been subscribed to yet via subscribe(), sleep for stream_sleep_no_subscriptions milliseconds.

async get_history(channel: str, limit: int | None = None) list[bytes][source]

Not implemented

class litestar.channels.backends.redis.RedisChannelsStreamBackend[source]

Bases: RedisChannelsBackend

__init__(history: int, *, redis: Redis, stream_sleep_no_subscriptions: int = 1, cap_streams_approximate: bool = True, stream_ttl: int | timedelta = datetime.timedelta(seconds=60), key_prefix: str = 'LITESTAR_CHANNELS') None[source]

Redis channels backend, streams.

Parameters:
  • history – Amount of messages to keep. This will set a MAXLEN to the streams

  • redis – A redis.asyncio.Redis instance

  • key_prefix – Key prefix to use for streams

  • stream_sleep_no_subscriptions – Amount of time in milliseconds to pause the stream_events() generator, should no subscribers exist

  • cap_streams_approximate – Set the streams MAXLEN using the ~ approximation operator for improved performance

  • stream_ttl – TTL of a stream in milliseconds or as a timedelta. A streams TTL will be set on each publishing operation using PEXPIRE

async on_startup() None[source]

Called on application startup

async on_shutdown() None[source]

Called on application shutdown

async subscribe(channels: Iterable[str]) None[source]

Subscribe to channels

async unsubscribe(channels: Iterable[str]) None[source]

Unsubscribe from channels

async publish(data: bytes, channels: Iterable[str]) None[source]

Publish data to channels.

Note

This operation is performed atomically, using a Lua script

async stream_events() AsyncGenerator[tuple[str, Any], None][source]

Return a generator, iterating over events of subscribed channels as they become available.

If no channels have been subscribed to yet via subscribe(), sleep for stream_sleep_no_subscriptions milliseconds.

async get_history(channel: str, limit: int | None = None) list[bytes][source]

Return the history of channels, returning at most limit messages

async flush_all() int[source]

Delete all stream keys with the key_prefix.

Important

This method is incompatible with redis clusters