gb2008-京淘day11
闪耀太阳 2020-12-08 11:57:15 1430 收藏 5
分类专栏: 正课 文章标签: redis
版权
- SpringBoot整合Redis
1.1 整合入门案例
1.1.1 导入jar包
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
10
1.1.2 完成测试案例
在这里插入图片描述
1.1.3 测试基本命令
package com.jt.test;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.SetParams;
//@SpringBootTest
public class TestRedis {
//链接服务: IP地址:端口
@Test
public void testSetGet() throws InterruptedException {
Jedis jedis = new Jedis("192.168.126.129",6379);
jedis.flushAll(); //先清空redis缓存
//1.存取redis
jedis.set("key1", "天天向上");
String value = jedis.get("key1");
System.out.println(value);
//2.判断key是否存在
if(jedis.exists("key1")){
jedis.set("key1", "好好学习,天天向上");
}else{
jedis.set("key1", "天天开心");
}
//3.为key添加超时时间
jedis.expire("key1", 10);
Thread.sleep(2000);
System.out.println("剩余存活时间:"+jedis.ttl("key1"));
//4.撤销剩余超时时间
jedis.persist("key1");
System.out.println("撤销成功!!");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
1.1.4 setNx/setEx/set命令
/**
* 需求: 如果数据不存在,则将数据修改
*/
@Test
public void testSetNx(){
Jedis jedis = new Jedis(“192.168.126.129”,6379);
jedis.set(“key1”, “aaa”);
//如果key不存在,则赋值
jedis.setnx(“key1”, “测试方法”);
System.out.println(jedis.get(“key1”));
}
/**
* 需求: 实现超时时间的设定与赋值操作的原子性.
* 考点: 为数据设定超时时间时,切记满足原子性要求.
* 否则会出现key永远不能删除的现象
*/
@Test
public void testSetEx(){
Jedis jedis = new Jedis("192.168.126.129",6379);
/*jedis.set("key2", "bbb");
jedis.expire("key2", 3);*/
//???数据超时之后一定会被删除吗??? 错的
jedis.setex("key2", 10, "bbb");
}
/**
* 需求: 如果数据存在时,才会修改数据,并且为数据添加超时时间10秒(原子性).
* 参数说明:
* NX: 只有数据不存在时,才会赋值.
* XX: 只有数据存在时,才会赋值.
* EX: 秒
* PX: 毫秒
*/
public void testSet(){
Jedis jedis = new Jedis("192.168.126.129",6379);
/* if(jedis.exists("key3")){
jedis.setex("key3",10 ,"ccc");
}*/
SetParams setParams = new SetParams();
setParams.xx().ex(10);
//将所有的操作采用原子性的方式进行控制
jedis.set("key3", "ccc", setParams);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
1.1.5 测试hash
/**
* HASH测试
/
@Test
public void testHash(){
Jedis jedis = new Jedis(“192.168.126.129”,6379);
jedis.hset(“person”, “id”, “100”);
jedis.hset(“person”, “name”, “tomcat猫”);
System.out.println(jedis.hgetAll(“person”));
}
1
2
3
4
5
6
7
8
9
10
1.1.6 测试List
/*
* LIST集合测试
/
@Test
public void testList(){
Jedis jedis = new Jedis(“192.168.126.129”,6379);
jedis.lpush(“list”, “1”,“2”,“3”,“4”);
String value = jedis.rpop(“list”);
System.out.println(value);
}
1
2
3
4
5
6
7
8
9
10
1.1.7 测试事务
/*
* Set集合测试
* 1. sadd 新增元素
* 2. SCARD 获取元素数量
* 3. SINTER key1 key2 获取元素的交集
* 4. SMEMBERS set 获取集合元素
* demo自己补一下
* * */
@Test
public void testMulti(){
Jedis jedis = new Jedis("192.168.126.129",6379);
//开启事务
Transaction transaction = jedis.multi();
try {
transaction.set("a", "a");
transaction.set("b", "b");
//提交事务
transaction.exec();
}catch (Exception e){
//回滚事务
transaction.discard();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1.2 SpringBoot整合Redis
1.2.1 编辑properties配置文件
在这里插入图片描述
1.2.2 编辑配置类
在这里插入图片描述