• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解SpringBoot2.0的@Cacheable(Redis)缓存失效时间解决方案

springboot 搞代码 4年前 (2022-01-05) 35次浏览 已收录 0个评论

这篇文章主要介绍了详解SpringBoot2.0的@Cacheable(Redis)缓存失效时间解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

问题

  @Cacheable注解不支持配置过期时间,所有需要通过配置CacheManneg来配置默认的过期时间和针对每个类或者是方法进行缓存失效时间配置。

解决

  可以采用如下的配置信息来解决的设置失效时间问题

配置信息

 @Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { return new RedisCacheManager( RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), this.getRedisCacheConfigurationWithTtl(30*60), // 默认策略,未配置的 key 会使用这个 this.getRedisCacheConfigurationMap() // 指定 key 策略 ); } private Map getRedisCacheConfigurationMap() { Map redisCacheConfigurationMap = new HashMap(); //SsoCache和BasicDataCache进行过期时间配置 redisCacheConfigurationMap.put("SsoCache", this.getRedisCacheConfigurationWithTtl(24*60*60)); redisCacheConfigurationMap.put("BasicDataCache", this.getRedisCacheConfigurationWithTtl(30*60)); return redisCacheConfigurationMap; } private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) { Jackson2JsonRedisSerializer 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.ofSeconds(seconds)); return redisCacheConfiguration; } @Bean public KeyGenerator wiselyKeyGenerator() { return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append("." + method.getName()); if(params==null||params.length==0||params[0]==null){ return null; } String join = String.join("&", Arrays.strea<strong style="color:transparent">来源gao@daima#com搞(%代@#码网</strong>m(params).map(Object::toString).collect(Collectors.toList())); String format = String.format("%s{%s}", sb.toString(), join); //log.info("缓存key:" + format); return format; } }; } 

使用方式

 @CacheConfig(cacheNames = "SsoCache") public class SsoCache{ @Cacheable(keyGenerator = "wiselyKeyGenerator") public String getTokenByGsid(String gsid) } //二者选其一,可以使用value上的信息,来替换类上cacheNames的信息 @Cacheable(value = "BasicDataCache",keyGenerator = "wiselyKeyGenerator") public String getTokenByGsid(String gsid) 

效果展示

到此这篇关于详解SpringBoot2.0的@Cacheable(Redis)缓存失效时间解决方案的文章就介绍到这了,更多相关SpringBoot2.0缓存失效内容请搜索gaodaima搞代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持gaodaima搞代码网

以上就是详解SpringBoot2.0的@Cacheable(Redis)缓存失效时间解决方案的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解SpringBoot2.0的@Cacheable(Redis)缓存失效时间解决方案

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址