SpringBoot 2.x 以后,@Cacheable, Redis-cahce 的配置变动比较大
网上找了点资料,目前我的配置是
@Bean public RedisCacheConfiguration redisCacheConfiguration() { Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig(); redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith( RedisSerializationContext .SerializationPair .fromSerializer(jackson2JsonRedisSerializer) ).entryTtl(Duration.ofMinutes(30)); return redisCacheConfiguration; }
使用上述代码后,可以成功缓存,但不能对key指定缓存时间,
如图,两个 key UserInfoList
和 key UserInfoListAnother
都是默认的30分钟
2.X以后无法再使用 RedisCacheManager rcm = new RedisCacheManager(redisTemplate)
构造方法来设置缓存时间,请问该如何配置?
相关分类