### 1.在redisConfig中开启缓存注解 **@EnableCaching**
~~~
@Configuration
@Slf4j
@EnableCaching
public class RedisConfig {
~~~
### 2.配置缓存管理器
~~~
//缓存管理器
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
//缓存配置对象
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(30L)) //设置缓存的默认超时时间:30分钟
.disableCachingNullValues() //如果是空值,不缓存
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getKeySerializer())) //设置key序列化器
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()));//设置value序列
return RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(configuration).build();
}
~~~