- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- 詳解SpringBoot2.0的@Cacheable(Redis)緩存失效時(shí)間解決
@Cacheable注解不支持配置過(guò)期時(shí)間,所有需要通過(guò)配置CacheManneg來(lái)配置默認的過(guò)期時(shí)間和針對每個(gè)類(lèi)或者是方法進(jìn)行緩存失效時(shí)間配置。
可以采用如下的配置信息來(lái)解決的設置失效時(shí)間問(wèn)題
配置信息
@Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { return new RedisCacheManager( RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), this.getRedisCacheConfigurationWithTtl(30*60), // 默認策略,未配置的 key 會(huì )使用這個(gè) this.getRedisCacheConfigurationMap() // 指定 key 策略 ); } private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() { Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(); //SsoCache和BasicDataCache進(jìn)行過(guò)期時(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上的信息,來(lái)替換類(lèi)上cacheNames的信息 @Cacheable(value = "BasicDataCache",keyGenerator = "wiselyKeyGenerator") public String getTokenByGsid(String gsid)
效果展示
到此這篇關(guān)于詳解SpringBoot2.0的@Cacheable(Redis)緩存失效時(shí)間解決方案的文章就介紹到這了,更多相關(guān)SpringBoot2.0緩存失效內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站