pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.properties
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=123456
使用的地方
@Resource
private RedisTemplate<String,Object> redisTemplate;
判断key是否存在
redisTemplate.hasKey("xxx")
存入缓存
redisTemplate.opsForValue().set("xxx", 数据)
超时时间(24小时,自己修改)
redisTemplate.expire("xxx", 24, TimeUnit.HOURS);
读取缓存
redisTemplate.opsForValue().get("xxx")
因为读取的为obj,项目需求为集合
Object obj = redisTemplate.opsForValue().get("xxx");
List<User> userList = new ArrayList();
for (Object o : (List<?>) obj) {
userList.add(User.class.cast(o));
}
return userList;
作者:鲨鱼辣椒灬
来源:优快云