列表命令
- 基本命令
- 入门
- Blpop 命令
- Brpop 命令
- Brpoplpush 命令
- Lindex 命令
- Linsert 命令
- Llen 命令
- Lpop 命令
- Lpush 命令
- Lpushx 命令
- Lrange命令
- Lrem 命令
- Lset
- Ltrim
- Rpop 命令
- Rpoplpush 命令
- Rpush 命令
- Rpushx 命令
- spring调用redis
- pom.xml配置 核心依赖
- list-redis.xml spring配置文件
- 测试代码1
- 测试代码2
- 可视化工具 redis-desktop-manager下载
- 可视化工具观察
基本命令
Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)
入门
localhost:6379> lpush pop a b
(integer) 2
localhost:6379> lpush pop c
(integer) 3
localhost:6379> lrange pop
(error) ERR wrong number of arguments for 'lrange' command
localhost:6379> lrange pop 0 4
1) "c"
2) "b"
3) "a"
Blpop 命令
移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。单位秒
localhost:6379> lrange pop 0 4
1) "c"
2) "b"
3) "a"
localhost:6379> blpop pop 20
1) "pop"
2) "c"
localhost:6379> lrange pop 0 4
1) "b"
2) "a"
Brpop 命令
移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
localhost:6379> lrange pop 0 10
1) "c"
2) "b"
3) "a"
localhost:6379> brpop list 10
(nil)
(10.01s)
localhost:6379> brpop pop 10
1) "pop"
2) "a"
localhost:6379> lrange pop 0 10
1) "c"
2) "b"
Brpoplpush 命令
从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
localhost:6379> lrange pop 0 4
1) "4"
2) "3"
3) "2"
4) "1"
localhost:6379> brpoplpush pop newpop 1
"1"
localhost:6379> lrange pop 0 4
1) "4"
2) "3"
3) "2"
localhost:6379> lrange newpop 0 4
1) "1"
Lindex 命令
用于通过索引获取列表中的元素。你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。
localhost:6379> lrange pop 0 4
1) "4"
2) "3"
3) "2"
4) "1"
localhost:6379> lindex pop 0
"4"
localhost:6379> lindex pop 3
"1"
localhost:6379> lindex pop -1
"1"
localhost:6379> lindex pop -2
"2"
Linsert 命令
用于在列表的元素前或者后插入元素。当指定元素不存在于列表中时,不执行任何操作。
当列表不存在时,被视为空列表,不执行任何操作。
如果 key 不是列表类型,返回一个错误。
localhost:6379> lrange pop 0 5
1) "4"
2) "3"
3) "2"
4) "1"
localhost:6379> linsert pop AFTER 4 5
(integer) 5
localhost:6379> lrange pop 0 10
1) "4"
2) "5"
3) "3"
4) "2"
5) "1"
localhost:6379> linsert pop before 1 1.5
(integer) 6
localhost:6379> lrange pop 0 10
1) "4"
2) "5"
3) "3"
4) "2"
5) "1.5"
6) "1"
Llen 命令
用于返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误。
localhost:6379> llen pop
(integer) 6
Lpop 命令
用于移除并返回列表的第一个元素。
localhost:6379> lrange pop 0 10
1) "4"
2) "5"
3) "3"
4) "2"
5) "1.5"
6) "1"
localhost:6379> lpop pop
"4"
Lpush 命令
将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时,返回一个错误。
localhost:6379> lpush pop 8 9
(integer) 7
localhost:6379> lrange pop 0 7
1) "9"
2) "8"
3) "5"
4) "3"
5) "2"
6) "1.5"
7) "1"
Lpushx 命令
将一个值插入到已存在的列表头部,列表不存在时操作无效。
localhost:6379> lpushx pop2 1
(integer) 0
localhost:6379> lpushx pop 1
(integer) 8
Lrange命令
返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。
localhost:6379> lrange pop 0 -1
1) "1"
2) "9"
3) "8"
4) "5"
5) "3"
6) "2"
7) "1.5"
8) "1"
Lrem 命令
根据参数 COUNT 的值,移除列表中与参数 VALUE 相等的元素。
COUNT 的值可以是以下几种:
• count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。
• count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。
• count = 0 : 移除表中所有与 VALUE 相等的值
localhost:6379> lrange pop 0 -1
1) "1"
2) "9"
3) "8"
4) "5"
5) "3"
6) "2"
7) "1.5"
8) "1"
localhost:6379> lrem pop -2 1
(integer) 2
localhost:6379> lrange pop 0 -1
1) "9"
2) "8"
3) "5"
4) "3"
5) "2"
6) "1.5"
localhost:6379> lrem pop 0 1.5
(integer) 1
Lset
通过索引来设置元素的值。
当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误。
localhost:6379> lrange pop 0 -1
1) "9"
2) "8"
3) "5"
4) "3"
5) "2"
localhost:6379> lset pop 4 1
OK
localhost:6379> lrange pop 0 -1
1) "9"
2) "8"
3) "5"
4) "3"
5) "1"
Ltrim
对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。
下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。
localhost:6379> lrange pop 0 -1
1) "9"
2) "8"
3) "5"
4) "3"
5) "1"
localhost:6379> ltrim pop 1 -2
OK
localhost:6379> lrange pop 0 -1
1) "8"
2) "5"
3) "3"
Rpop 命令
用于移除列表的最后一个元素,返回值为移除的元素。
localhost:6379> lrange pop 0 -1
1) "8"
2) "5"
3) "3"
localhost:6379> rpop pop
"3"
localhost:6379> lrange pop 0 -1
1) "8"
2) "5"
Rpoplpush 命令
用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回。
localhost:6379> lrange pop 0 -1
1) "8"
2) "5"
localhost:6379> lrange pop2 0 -1
(empty list or set)
localhost:6379> rpoplpush pop pop2
"5"
localhost:6379> lrange pop2 0 -1
1) "5"
localhost:6379> lrange pop 0 -1
1) "8"
Rpush 命令
用于将一个或多个值插入到列表的尾部(最右边)。
如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。
localhost:6379> lrange pop 0 -1
1) "8"
localhost:6379> rpush pop 9
(integer) 2
localhost:6379> rpush pop 10
(integer) 3
localhost:6379> lrange pop 0 -1
1) "8"
2) "9"
3) "10"
Rpushx 命令
用于将一个值插入到已存在的列表尾部(最右边)。如果列表不存在,操作无效。
localhost:6379> lrange pop 0 -1
1) "8"
2) "9"
3) "10"
localhost:6379> rpushx pop 11
(integer) 4
localhost:6379> rpushx pop22 2
(integer) 0
localhost:6379> lrange pop 0 -1
1) "8"
2) "9"
3) "10"
4) "11"
spring调用redis
pom.xml配置 核心依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- slf4j日志统一管理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<!-- spring中的redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.4.RELEASE</version>
</dependency>
list-redis.xml spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd"
default-lazy-init="false">
<!-- 占位符配置文件 -->
<!-- <context:property-placeholder location="classpath:redis.properties" /> -->
<!-- redis 连接池配置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="50" />
<property name="maxTotal" value="100" />
<property name="maxWaitMillis" value="20000" />
</bean>
<!-- Spring-redis连接池工厂配置 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1" />
<property name="port" value="6379" />
<property name="timeout" value="2000" />
<property name="poolConfig" ref="poolConfig" />
</bean>
<!-- 序列化 String类型 -->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<!-- 序列化 对象 -->
<!-- <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> -->
<!-- redisTemplate 定义 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="valueSerializer" ref="stringRedisSerializer" />
</bean>
</beans>
测试代码1
package redis.list;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.connection.RedisListCommands.Position;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
/**
*
* 非阻塞命令,同时也是线程不安全的
*
* @author it
*/
public class RedisListTest {
private static final Logger logger = LoggerFactory.getLogger(RedisListTest.class);
public static void main(String[] args) throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("redis-conf/linkedlist/list-redis.xml");
RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
ListOperations opsForList = redisTemplate.opsForList();
// left表示最前面的元素,right表示最末端的元素
// 1.删除,重复测试
redisTemplate.delete("list");
// 2.放入单个元素
opsForList.leftPush("list", "node3");
// 3.放入多个元素
List list = new ArrayList();
list.add("node2");
list.add("node1");
opsForList.leftPushAll("list", list);
// 4.末端插入元素
opsForList.rightPush("list", "node4");
// 5.获取下标为0的节点
String indexStr0 = (String) opsForList.index("list", 0);
logger.debug("获取list下标为0的节点值:{}", indexStr0);
// 6.list长度
Long size = opsForList.size("list");
logger.debug("list长度值:{}", size);
// 7.从左边弹出一个节点
String element = (String) opsForList.leftPop("list");
logger.debug("元素element值:{}", element);
// 8.从右边弹出一个节点
element = (String) opsForList.rightPop("list");
logger.debug("元素element值:{}", element);
size = opsForList.size("list");
logger.debug("list长度值:{}", size);
// 9.在node2前插入一个节点
redisTemplate.getConnectionFactory().getConnection().lInsert("list".getBytes("utf-8"), Position.BEFORE, "node2".getBytes("utf-8"), "node0".getBytes("utf-8"));
// 10.在node2后插入一个节点
redisTemplate.getConnectionFactory().getConnection().lInsert("list".getBytes("utf-8"), Position.AFTER, "node2".getBytes("utf-8"), "node2.5".getBytes("utf-8"));
// 11.如果list存在,左端插入一个节点
opsForList.leftPushIfPresent("list", "head");
// 12.如果list存在,右端插入一个节点
opsForList.rightPushIfPresent("list", "end");
// 13.截取list元素
List range = opsForList.range("list", 1, -1);
logger.debug("range值[没有head元素,head下标为0]:{}", range);
// 13.删除所有end元素 count = 0 : 移除表中所有与 VALUE 相等的值
opsForList.remove("list", 0, "end");
// 14.输出所有list值
range = opsForList.range("list", 0, -1);
logger.debug("所有list值:{}", range);
}
}
测试代码2
package redis.list;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
/**
*
* 阻塞命令,同时也是线程安全的,一定程度能保证数据而性能不佳
*
* @author it
*/
public class RedisListBlockTest {
private static final Logger logger = LoggerFactory.getLogger(RedisListBlockTest.class);
public static void main(String[] args) throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("redis-conf/linkedlist/list-redis.xml");
RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
ListOperations opsForList = redisTemplate.opsForList();
// left表示最前面的元素,right表示最末端的元素
// 1.删除,重复测试
redisTemplate.delete("list1");
redisTemplate.delete("list2");
List<String> nodeList = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
nodeList.add("node" + i);
}
opsForList.leftPushAll("list1", nodeList);
String leftPop = (String) opsForList.leftPop("list1", 1, TimeUnit.SECONDS);
String rightPop = (String) opsForList.rightPop("list1", 1, TimeUnit.SECONDS);
logger.debug("leftPop值:{}", leftPop);
logger.debug("rightPop值:{}", rightPop);
nodeList.clear();
for (int i = 1; i <= 3; i++) {
nodeList.add("data" + i);
}
opsForList.leftPushAll("list2", nodeList);
logger.debug("list1值:{}", opsForList.range("list1", 0, -1));
logger.debug("list2值:{}", opsForList.range("list2", 0, -1));
// 相当于rpoplpush
opsForList.rightPopAndLeftPush("list1", "list2");
// 相当于brpoplpush
opsForList.rightPopAndLeftPush("list1", "list2", 1, TimeUnit.SECONDS);
logger.debug("list1值:{}", opsForList.range("list1", 0, -1));
logger.debug("list2值:{}", opsForList.range("list2", 0, -1));
}
}
可视化工具 redis-desktop-manager下载
redis-desktop-manager-0.9.99.zip工具下载:
链接:https://pan.baidu.com/s/1BB-MvTfx6T1SDD_eq5tq5w 提取码:snby
可视化工具观察