redis

class litestar.channels.backends.redis.RedisChannelsBackend

Bases: ChannelsBackend, ABC

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

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

Bases: RedisChannelsBackend

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

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

Called by the plugin on application startup

async on_shutdown() None

Called by the plugin on application shutdown

async subscribe(channels: Iterable[str]) None

Subscribe to channels, and enable publishing to them

async unsubscribe(channels: Iterable[str]) None

Stop listening for events on channels

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

Publish data to channels

Note

This operation is performed atomically, using a lua script

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

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]

Not implemented

class litestar.channels.backends.redis.RedisChannelsStreamBackend

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

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

Called on application startup

async on_shutdown() None

Called on application shutdown

async subscribe(channels: Iterable[str]) None

Subscribe to channels

async unsubscribe(channels: Iterable[str]) None

Unsubscribe from channels

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

Publish data to channels.

Note

This operation is performed atomically, using a Lua script

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

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]

Return the history of channels, returning at most limit messages

async flush_all() int

Delete all stream keys with the key_prefix.

Important

This method is incompatible with redis clusters