一、添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
二、application.yml
spring:
redis:
host: 192.168.25.129
三、注入RedisTemplate对象
@Autowired private RedisTemplate redisTemplate;
四、缓存操作
1.从redis取内容
Article article = (Article)redisTemplate.opsForValue().get("article_"+id);
2.redis存内容
redisTemplate.opsForValue().set("article_" + id, article);
3.删缓存
redisTemplate.delete("article_" + id);
4.设置缓存过期时间
redisTemplate.opsForValue().set("article_" + id, article,1,TimeUnit.DAYS);