詳解SpringBoot2.0的@Cacheable(Redis)緩存失效時(shí)間解決方案
問題
@Cacheable注解不支持配置過期時(shí)間,所有需要通過配置CacheManneg來配置默認(rèn)的過期時(shí)間和針對(duì)每個(gè)類或者是方法進(jìn)行緩存失效時(shí)間配置。
解決
可以采用如下的配置信息來解決的設(shè)置失效時(shí)間問題
配置信息
@Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { return new RedisCacheManager( RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), this.getRedisCacheConfigurationWithTtl(30*60), // 默認(rèn)策略,未配置的 key 會(huì)使用這個(gè) this.getRedisCacheConfigurationMap() // 指定 key 策略 ); } private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() { Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(); //SsoCache和BasicDataCache進(jìn)行過期時(shí)間配置 redisCacheConfigurationMap.put("SsoCache", this.getRedisCacheConfigurationWithTtl(24*60*60)); redisCacheConfigurationMap.put("BasicDataCache", this.getRedisCacheConfigurationWithTtl(30*60)); return redisCacheConfigurationMap; } private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) { 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.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.stream(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)
效果展示
到此這篇關(guān)于詳解SpringBoot2.0的@Cacheable(Redis)緩存失效時(shí)間解決方案的文章就介紹到這了,更多相關(guān)SpringBoot2.0緩存失效內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot+SpringSecurity+jwt實(shí)現(xiàn)驗(yàn)證
本文主要介紹了SpringBoot+SpringSecurity+jwt實(shí)現(xiàn)驗(yàn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Spring Data Jpa+SpringMVC+Jquery.pagination.js實(shí)現(xiàn)分頁示例
本文介紹了Spring Data Jpa+SpringMVC+Jquery.pagination.js實(shí)現(xiàn)分頁示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12淺談java中null是什么,以及使用中要注意的事項(xiàng)
下面小編就為大家?guī)硪黄獪\談java中null是什么,以及使用中要注意的事項(xiàng)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09java日志LoggerFactory.getLogger的用法及說明
這篇文章主要介紹了java日志LoggerFactory.getLogger的用法及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02