Redis-SpringDataRedis快速入门

  1. 1. 配置文件application.yml
  2. 2. 编写测试类执行测试方法
  3. 3. 输出

1
2
3
4
5
<!--连接池依赖-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>

配置文件application.yml

1
2
3
4
5
6
7
8
9
10
11
spring:
redis:
host: 192.168.230.88 #指定redis所在的host
port: 6379 #指定redis的端口
password: 132537 #设置redis密码
lettuce:
pool:
max-active: 8 #最大连接数
max-idle: 8 #最大空闲数
min-idle: 0 #最小空闲数
max-wait: 100ms #连接等待时间

编写测试类执行测试方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@SpringBootTest
class RedisSpringbootApplicationTests {
@Autowired
private RedisTemplate redisTemplate;

@Test
void testString() {
// 1. 通过RedisTemplate获取操作的String类型的valueOperations对象
ValueOperations ops = redisTemplate.opsForValue();
// 2. 插入一条数据
ops.set("msg1", "hello jedis");
// 3. 获取数据
String msg = (String) ops.get("msg");
System.out.println("msg="+msg);
}

}

输出

msg=hello jedis