SpringBoot使用Redis实现 自动缓存 更新 删除
本篇主要讲述如何使用基本的注解 @Cacheable @CachePut @CacheEvict 操作缓存
1.我们导入Redis的依赖
<!--这里Redis我给了版本-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!--使用配置类时,防止乱码需要用到的包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2.编写配置类
@ConfigurationProperties(prefix = "spring.cache.redis")
@Configuration
public class RedisConfig {
private Duration timeToLive = Duration.ZERO;
public void setTimeToLive(Duration timeToLive) {
this.timeToLive = timeToLive;
}
@Bean
/**
* 该bean只针对cache存入到数据乱码问题
*/
public RedisCacheManager cacheManager(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer