/**
* Redis缓存配置。
* @author zxp
*/
@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {
@Autowired
private RedisConnectionFactory factory;
/**
* 缓存过期时间
*/
public static final long CACHE_EXPIRE_SECEND = 3600 * 24;
@Override
public CacheManager cacheManager() {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(CACHE_EXPIRE_SECEND))
// 序列化设置
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.string()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
.disableCachingNullValues();
return RedisCacheManager.builder(factory)
.cacheDefaults(config)
.transactionAware()
.build();
}
}
再在要缓存的方法上加上注解@Cacheable