Springboot集成redis
# pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
1
2
3
4
2
3
4
# yml文件
spring:
redis:
database: 0
port: 6379
pool:
max-idle: 8
min-idle: 0
max-active: 8
max-wait: -1
host: 192.168.213.213
password: redismima123
timeout: 100
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 代码使用
# String类型:
public class imclass {
@Autowired
private StringRedisTemplate Redis;
public String test(String rawPass) {
//存储
Redis.opsForValue().set(key, value, 1800,TimeUnit.SECONDS);
//取值
String str = Redis.opsForValue().get(key);
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# Object类型:
public class imclass {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public String test(String rawPass) {
//存储
Map<String,Object> params = new HashMap<>();
params.put("accountId", "2");
params.put("limit", 1);
List<EvrAlarm> evrAlarms=evrAlarmDao.selectEvrAlarmByAccount(params);
//redis缓存
ListOperations<String, Object> lo = redisTemplate.opsForList();
lo.rightPush("EvrAlarm-2", evrAlarms);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
上次更新: 2023/03/10, 09:02:56