文章目录
- Redis之list类型
- 1. 列表添加/弹出元素
- 2. 查看列表
- 3. 获取列表中元素的个数
- 4. 删除列表中指定的值
- 5. 获取/指定元素的值
- 6. 向列表中插入元素
- 7. 删除指定索引范围之外的所有元素
- 8. 将元素从一个列表转移到另一个列表
- 9. 应用场景
- 9.1 队列
- 9.2 类似微信上订阅公众号(或者喜欢的文章)
Redis之list类型
1. 列表添加/弹出元素
- 向列表左边添加元素:LPUSH key value [value …]
- 向列表右边添加元素:RPUSH key value [value…]
- 从列表左边弹出元素:LPOP key
- 从列表右边弹出元素:RPOP key
127.0.0.1:6379> lpush books C
(integer) 1
127.0.0.1:6379> lpush books C++ JAVA
(integer) 3
127.0.0.1:6379> lrange books 0 -1
1) "JAVA"
2) "C++"
3) "C"
127.0.0.1:6379> rpush books Python Golang
(integer) 5
127.0.0.1:6379> lrange books 0 -1
1) "JAVA"
2) "C++"
3) "C"
4) "Python"
5) "Golang"
127.0.0.1:6379> rpop books
"Golang"
127.0.0.1:6379> lrange books 0 -1
1) "JAVA"
2) "C++"
3) "C"
4) "Python"
127.0.0.1:6379> lpop books
"JAVA"
127.0.0.1:6379> lrange books 0 -1
1) "C++"
2) "C"
3) "Python"
2. 查看列表
LRANGE key start end
end为负数时候,-1是倒数第一个,-2是倒数第二个...
127.0.0.1:6379> lrange books 0 -1
1) "C++"
2) "C"
3) "Python"
127.0.0.1:6379> lrange books 0 -2
1) "C++"
2) "C"
127.0.0.1:6379> lrange books 0 1
1) "C++"
2) "C"
127.0.0.1:6379> lrange books 0 2
1) "C++"
2) "C"
3) "Python"
127.0.0.1:6379> lrange books 0 3
1) "C++"
2) "C"
3) "Python"
3. 获取列表中元素的个数
LLEN key
127.0.0.1:6379> lrange books 0 -1
1) "C++"
2) "C"
3) "Python"
127.0.0.1:6379> llen books
(integer) 3
4. 删除列表中指定的值
LREM key count value
- count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。
- count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。
- count = 0 : 移除表中所有与 VALUE 相等的值。
127.0.0.1:6379> lrange books 0 -1
1) "C++"
2) "C"
3) "Python"
127.0.0.1:6379> rpush books Java C C Python
(integer) 7
127.0.0.1:6379> lrange books 0 -1
1) "C++"
2) "C"
3) "Python"
4) "Java"
5) "C"
6) "C"
7) "Python"
127.0.0.1:6379> lrem books 2 C
(integer) 2
127.0.0.1:6379> lrange books 0 -1
1) "C++"
2) "Python"
3) "Java"
4) "C"
5) "Python"
127.0.0.1:6379> lrem books 0 Python
(integer) 2
127.0.0.1:6379> lrange books 0 -1
1) "C++"
2) "Java"
3) "C"
127.0.0.1:6379> lpush books Java
(integer) 4
127.0.0.1:6379> lrange books 0 -1
1) "Java"
2) "C++"
3) "Java"
4) "C"
127.0.0.1:6379> lrem books -1 Java
(integer) 1
127.0.0.1:6379> lrange books 0 -1
1) "Java"
2) "C++"
3) "C"
5. 获取/指定元素的值
获取索引元素的值:LINDEX key index
指定索引元素的值:LSET key index value
127.0.0.1:6379> lrange books 0 -1
1) "Java"
2) "C++"
3) "C"
127.0.0.1:6379> lindex books 0
"Java"
127.0.0.1:6379> lindex books 1
"C++"
127.0.0.1:6379> lset books 1 Python
OK
127.0.0.1:6379> lindex books 1
"Python"
127.0.0.1:6379> lrange books 0 -1
1) "Java"
2) "Python"
3) "C"
6. 向列表中插入元素
LINSERT key BEFORE|AFTER pivot value
127.0.0.1:6379> lrange books 0 -1
1) "Java"
2) "Python"
3) "C"
127.0.0.1:6379> linsert books before C Golang
(integer) 4
127.0.0.1:6379> lrange books 0 -1
1) "Java"
2) "Python"
3) "Golang"
4) "C"
127.0.0.1:6379> linsert books after C Java
(integer) 5
127.0.0.1:6379> lrange books 0 -1
1) "Java"
2) "Python"
3) "Golang"
4) "C"
5) "Java"
7. 删除指定索引范围之外的所有元素
LTRIM key start end
127.0.0.1:6379> lrange books 0 -1
1) "Java"
2) "Python"
3) "Golang"
4) "C"
5) "Java"
127.0.0.1:6379> ltrim books 0 -2
OK
127.0.0.1:6379> lrange books 0 -1
1) "Java"
2) "Python"
3) "Golang"
4) "C"
8. 将元素从一个列表转移到另一个列表
RPOPLPUSH source destination
127.0.0.1:6379> rpoplpush books language
"C"
127.0.0.1:6379> lrange books 0 -1
1) "Java"
2) "Python"
3) "Golang"
127.0.0.1:6379> lrange language 0 -1
1) "C"
127.0.0.1:6379> rpoplpush books language
"Golang"
127.0.0.1:6379> lrange books 0 -1
1) "Java"
2) "Python"
127.0.0.1:6379> lrange language 0 -1
1) "Golang"
2) "C"
9. 应用场景
9.1 队列
使用LPUSH+RPOP或者RPOP+LPUSH可以模拟队列操作,具体请参考我之前博客
9.2 类似微信上订阅公众号(或者喜欢的文章)
看到喜欢的文章,对该订阅号点个关注,把该公众号主键放到list中去,然后点开订阅号,可以看到自己关注的所有的订阅号,也可以让一次显示10条。
具体的redis操作:
127.0.0.1:6379> lpush gzdyh:user1001 dyh1
(integer) 1
127.0.0.1:6379> lpush gzdyh:user1001 dyh2 dyh3
(integer) 3
127.0.0.1:6379> lrange gzdyh:user1001 0 -1
1) "dyh3"
2) "dyh2"
3) "dyh1"
127.0.0.1:6379> lrange gzdyh:user1001 0 10
1) "dyh3"
2) "dyh2"
3) "dyh1"