*Redis BRPOPLPUSH 命令

阻塞式原子地将一个 List 的尾部元素移动到另一个 List 的头部。当 source List 为空时阻塞等待。


*语法

BRPOPLPUSH source destination timeout

*参数说明

参数 类型 必填 说明
source String 源 List 键名
destination String 目标 List 键名
timeout Integer/Float 阻塞超时时间(秒),0 表示永久阻塞

*返回值

条件 返回值
source 非空 被移动的元素(String)
source 为空且超时 nil

*时间复杂度

O(1)(非空时),阻塞期间不消耗 CPU。


*示例

> RPUSH queue task1 task2
(integer) 2
> BRPOPLPUSH queue processing 30
"task2"

> LRANGE queue 0 -1
1) "task1"
> LRANGE processing 0 -1
1) "task2"

# 空队列阻塞
> BRPOPLPUSH empty_queue dest 2
(nil)

*常见错误

  1. 对非 List 使用:返回 WRONGTYPE。

*最佳实践

  1. 可靠队列:BRPOPLPUSH 将任务从队列移动到处理队列,原子操作保证不丢失。
  2. 已被 BLMOVE 替代:Redis 6.2+ 推荐使用 BLMOVE source destination RIGHT LEFT timeout 替代 BRPOPLPUSH。

*FAQ

Q: BRPOPLPUSH 和 BLMOVE 有什么区别? A: BRPOPLPUSH 是 BLMOVE source destination RIGHT LEFT 的别名。Redis 6.2+ 推荐统一使用 BLMOVE