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
instancekey_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
instancekey_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 subscribe(channels: Iterable[str]) None #
Subscribe to
channels
, and enable publishing to them
- async publish(data: bytes, channels: Iterable[str]) None #
Publish
data
tochannels
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 forstream_sleep_no_subscriptions
milliseconds.
- 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 streamsredis¶ – A
redis.asyncio.Redis
instancekey_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 existcap_streams_approximate¶ – Set the streams
MAXLEN
using the~
approximation operator for improved performancestream_ttl¶ – TTL of a stream in milliseconds or as a timedelta. A streams TTL will be set on each publishing operation using
PEXPIRE
- async publish(data: bytes, channels: Iterable[str]) None #
Publish
data
tochannels
.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 forstream_sleep_no_subscriptions
milliseconds.