redis之List类型常用方法总结
redis之List类型常用方法总结
格式:
存---LPUSH key value [value ...]
取--LRANGE key start stop
lpush key value [value ...] 将一个或多个值插入到列表头部
127.0.0.1:6379> lpush plf panlifu
(integer) 1
127.0.0.1:6379> lpush plf lt
(integer) 2
127.0.0.1:6379> lpush plf xs
(integer) 3
127.0.0.1:6379> lpush plf cd
(integer) 4
127.0.0.1:6379> lrange plf 0 1
1) "cd"
2) "xs"
127.0.0.1:6379> lrange plf 0 3
1) "cd"
2) "xs"
3) "lt"
4) "panlifu"
lrange key start stop 获取列表指定范围内的元素
127.0.0.1:6379> lpush plf panlifu
(integer) 1
127.0.0.1:6379> lpush plf lt
(integer) 2
127.0.0.1:6379> lpush plf xs
(integer) 3
127.0.0.1:6379> lpush plf cd
(integer) 4
127.0.0.1:6379> lrange plf 0 1
1) "cd"
2) "xs"
127.0.0.1:6379> lrange plf 0 3
1) "cd"
2) "xs"
3) "lt"
4) "panlifu"
blpop key [key ...] timeout 移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
127.0.0.1:6379> blpop plf timeout 1
1) "plf"
2) "cd"
127.0.0.1:6379> lrange plf 0 4
1) "xs"
2) "lt"
3) "panlifu"
brpop key [key ...] timeout 移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
127.0.0.1:6379> lrange plf 0 4
1) "xs"
2) "lt"
3) "panlifu"
127.0.0.1:6379> brpop plf timeout 1
1) "plf"
2) "panlifu"
127.0.0.1:6379> lrange plf 0 4
1) "xs"
2) "lt"
brpoplpush source destination timeout 从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。?
127.0.0.1:6379> brpoplpush plf lt timeout 10
(error) ERR wrong number of arguments for 'brpoplpush' command
127.0.0.1:6379>lindex key index 通过索引获取列表中的元素
127.0.0.1:6379> lrange plf 0 3
1) "xs"
2) "lt"
127.0.0.1:6379> lindex plf 1
"lt"
127.0.0.1:6379> lindex plf 0
"xs"
127.0.0.1:6379>
linsert key BEFORE|AFTER pivot value 在列表的元素前或者后插入元素
127.0.0.1:6379> lrange plf 0 3
1) "xs"
2) "lt"
127.0.0.1:6379> linsert plf after lt hehe
(integer) 3
127.0.0.1:6379> lrange plf 0 3
1) "xs"
2) "lt"
3) "hehe"
127.0.0.1:6379> linsert plf before lt hehe
(integer) 4
127.0.0.1:6379> lrange plf 0 3
1) "xs"
2) "hehe"
3) "lt"
4) "hehe"
llen key 获取列表长度
127.0.0.1:6379> lrange plf 0 3
1) "xs"
2) "hehe"
3) "lt"
4) "hehe"
127.0.0.1:6379> llen plf
(integer) 4
lpop key 移出并获取列表的第一个元素
127.0.0.1:6379> lrange plf 0 3
1) "xs"
2) "hehe"
3) "lt"
4) "hehe"
127.0.0.1:6379> llen plf
(integer) 4
127.0.0.1:6379> lpop plf
"xs"
127.0.0.1:6379> lrange plf 0 3
1) "hehe"
2) "lt"
3) "hehe"
lpushx key value 将一个值插入到已存在的列表头部
```shell
127.0.0.1:6379> lpushx hehe hello
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> lpushx plf hello
(integer) 4
127.0.0.1:6379> lrange plf 0 5
1) "hello"
2) "hehe"
3) "lt"
4) "hehe"
```
lrem key count value 移除列表元素,count次数
127.0.0.1:6379> lrange plf 0 5
1) "hello"
2) "hehe"
3) "lt"
4) "hehe"
127.0.0.1:6379> lrem plf 2 hehe
(integer) 2
127.0.0.1:6379> lrange plf 0 5
1) "hello"
2) "lt"
lset key index value 通过索引设置列表元素的值
127.0.0.1:6379> lrange plf 0 5
1) "hello"
2) "lt"
127.0.0.1:6379> lset plf 0 gunkai
OK
127.0.0.1:6379> lrange plf 0 5
1) "gunkai"
2) "lt"
ltrim key start stop 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。
127.0.0.1:6379> lrange plf 0 10
1) "gunkai"
2) "lt"
3) "1"
4) "2"
5) "3"
6) "4"
7) "5"
8) "6"
9) "7"
10) "8"
127.0.0.1:6379> ltrim plf 0 3
OK
127.0.0.1:6379> lrange plf 0 10
1) "gunkai"
2) "lt"
3) "1"
4) "2"
rpop key 移除列表的最后一个元素,返回值为移除的元素。
127.0.0.1:6379> lrange plf 0 10
1) "gunkai"
2) "lt"
3) "1"
4) "2"
127.0.0.1:6379> rpop plf
"2"
127.0.0.1:6379> lrange plf 0 10
1) "gunkai"
2) "lt"
3) "1"
rpoplpush source destination 移除列表的最后一个元素,并将该元素添加到另一个列表并返回
127.0.0.1:6379> lrange plf 0 10
1) "gunkai"
2) "lt"
3) "1"
127.0.0.1:6379> rpoplpush plf lt
"1"
127.0.0.1:6379> lrange plf 0 10
1) "gunkai"
2) "lt"
127.0.0.1:6379> lrange lt 0 10
1) "1"
rpush key value [value ...] 在列表中添加一个或多个值
127.0.0.1:6379> lrange plf 0 5
1) "gunkai"
2) "lt"
127.0.0.1:6379> rpush plf 1 2 3 4 5 6 7 8
(integer) 10
127.0.0.1:6379> lrange plf 0 10
1) "gunkai"
2) "lt"
3) "1"
4) "2"
5) "3"
6) "4"
7) "5"
8) "6"
9) "7"
10) "8"
rpushx key value 为已存在的列表添加值
127.0.0.1:6379> rpushx test_key 1
(integer) 0
127.0.0.1:6379> lrange test_key 0 1
(empty list or set)
总结:以x结尾都是判断 key是否存在,不存在就不能设置,存在才能设置。以r开头表示可以同时添加多个值
最新文章
- 在Ubuntu上搭建Hadoop群集
- HDOJ 2089 不要62
- 提取安卓手机的recovery
- 黄聪:禁止wordpress版本自动升级的解决方案
- 如何切换android的横屏与竖屏?
- uoj164. 【清华集训2015】V 统计
- HOW TO: How to import UUID function into Postgre 9.3
- inline函数不能在for循环中使用的原因
- Spring MVC中使用POI导出Word
- JVM学习笔记一:Java运行时数据区域
- 一种HBase表数据迁移方法的优化
- LiveCharts文档-3开始-2基础
- powerdessigner使用教程
- nc命令简介
- sourceTree 基础使用
- Android 获取ROOT权限原理解析
- 2016.07.13-map的使用(以leetcode1-Two Sum为例)
- C++ 模板类友元之输出流操作符重载
- Mac 10.12安装迅雷2.7.2
- SDR软件无线电知识要点(三)EVM