(二) spring-boot使用redis

引入组件

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

添加配置

1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
redis:
database: 0 # Redis数据库索引(默认为0)
host: 127.0.0.1 # Redis服务器地址
port: 6379 # Redis服务器连接端口
password: # Redis服务器连接密码(默认为空)
timeout: 5000 # 连接超时时间(毫秒)
jedis:
pool:
max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 8 # 连接池中的最大空闲连接
min-idle: 0 # 连接池中的最小空闲连接

添加测试接口

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
package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/redis")
public class DebugRedis {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@RequestMapping("/set")
public Map<String, Object> function(String key, String value)
{
this.stringRedisTemplate.opsForValue().set(key, value);
Map<String, Object> result = new HashMap<>();
result.put("code", 0);
result.put("message", "success");
result.put("exec_result", 1);
return result;
}

@RequestMapping("/get")
public Map<String, Object> function(String key) {
Map<String, Object> result = new HashMap<>();
result.put("code", 0);
result.put("message", "success");
result.put("exec_result", this.stringRedisTemplate.opsForValue().get(key));
return result;
}
}

测试

设置key: /api/redis/set?key=name&value=spring-boot
读取key: /api/redis/get?key=name


(二) spring-boot使用redis
http://www.zhangdeman.cn/archives/de925df7.html
作者
白茶清欢
发布于
2022年1月18日
许可协议