redis¶
- class litestar.channels.backends.redis.RedisChannelsBackend¶
基类:
ChannelsBackend,ABC
- class litestar.channels.backends.redis.RedisChannelsPubSubBackend¶
-
- __init__(*, redis: Redis, stream_sleep_no_subscriptions: int = 1, key_prefix: str = 'LITESTAR_CHANNELS') None¶
Redis 频道后端,Pub/Sub。 该后端提供低开销和资源占用,但不支持历史记录。 Args: redis: 一个
redis.asyncio.Redis实例 key_prefix: 用于在 Redis 中存储数据的键前缀 stream_sleep_no_subscriptions: 当不存在订阅者时,暂停stream_events()生成器的时间(毫秒)
- async publish(data: bytes, channels: Iterable[str]) None¶
将
data发布到channels.. note:: 此操作使用 Lua 脚本原子性地执行
- async stream_events() AsyncGenerator[tuple[str, Any], None]¶
返回一个生成器,迭代订阅通道的可用事件。 如果尚未通过
subscribe()订阅任何通道,则休眠stream_sleep_no_subscriptions毫秒。
- class litestar.channels.backends.redis.RedisChannelsStreamBackend¶
-
- __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 频道后端,基于 streams。 Args: history: 保留的消息数量。这将为流(streams)设置
MAXLEN。 redis: 一个redis.asyncio.Redis实例。 key_prefix: 用于流(streams)的键前缀。 stream_sleep_no_subscriptions: 当不存在订阅者时,暂停stream_events()生成器的时间(毫秒)。 cap_streams_approximate: 使用~近似运算符设置流的MAXLEN,以提升性能。 stream_ttl: 流的生存时间(TTL),单位为毫秒或 timedelta 对象。每次发布操作时,将使用PEXPIRE设置流的 TTL。
- async publish(data: bytes, channels: Iterable[str]) None¶
将
data发布到channels。 .. note:: 此操作使用 Lua 脚本原子性地执行。
- async stream_events() AsyncGenerator[tuple[str, Any], None]¶
返回一个生成器,迭代订阅通道的可用事件。 如果尚未通过
subscribe()订阅任何通道,则休眠stream_sleep_no_subscriptions毫秒。