Spring @Cacheable自定義緩存過期時間的實現(xiàn)示例
實現(xiàn)效果
原來的@Cacheable使用方式:
@Cacheable(value = "userinfo", key = "#dto.userId")
實現(xiàn)后的使用方式:
@Cacheable(value = "userinfo#30#m", key = "#dto.userId")
實現(xiàn)代碼
創(chuàng)建一個自定義的緩存管理器,繼承自RedisCacheManager
public class CustomRedisCacheManager extends RedisCacheManager { public CustomRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) { super(cacheWriter, defaultCacheConfiguration); } /** * 重寫createRedisCache方法 * @param name 原來的name只是作為redis存儲鍵名 * 重寫的name可通過"#"拼接過期時間: * 1. 如果沒有"#"則默認不設(shè)置過期時間 * 2. 拼接的第一個"#"后面為過期時間,第二個"#"后面為時間單位 * 3. 時間單位的表示使用: d(天)、h(小時)、m(分鐘)、s(秒), 默認為h(小時) * @param cacheConfig * @return */ @Override protected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) { // 解析name,設(shè)置過期時間 if (StringUtils.isNotEmpty(name) && name.contains("#")) { String[] split = name.split("#"); // 緩存鍵名 String cacheName = split[0]; // "#"后第一位是時間 int expire = Integer.parseInt(split[1]); // 過期時間,默認為h(小時) Duration duration = Duration.ofHours(expire); // 根據(jù)"#"后第二位字符判斷過期時間的單位,設(shè)置相應(yīng)的過期時間,默認時間單位是h(小時) if (split.length == 3) { switch (split[2]){ case "d": duration = Duration.ofDays(expire); break; case "m": duration = Duration.ofMinutes(expire); break; case "s": duration = Duration.ofSeconds(expire); break; default: duration = Duration.ofHours(expire); } } return super.createRedisCache(cacheName, cacheConfig.entryTtl(duration)); } return super.createRedisCache(name, cacheConfig); } }
在redis配置類中,將上面自定義的緩存管理器注冊為Bean
@Configuration @EnableCaching public class RedisConfig { /** * 自定義RedisTemplate * 設(shè)置Redis序列化方式,默認使用的是JDKSerializer的序列化方式,效率低,所以這里設(shè)置使用FastJsonRedisSerializer * @param connectionFactory * @return */ @Bean @SuppressWarnings(value = {"unchecked", "rawtypes"}) public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); // 設(shè)置redis連接(LettuceConnectionFactory實現(xiàn)了RedisConnectionFactory) redisTemplate.setConnectionFactory(connectionFactory); FastJsonRedisSerializer serializer = new FastJsonRedisSerializer(Object.class); // key設(shè)置StringRedisSerializer序列化 redisTemplate.setKeySerializer(new StringRedisSerializer()); // value設(shè)置FastJsonRedisSerializer序列化 redisTemplate.setValueSerializer(serializer); // Hash key設(shè)置序列化 redisTemplate.setHashKeySerializer(new StringRedisSerializer()); // Hash value設(shè)置序列化 redisTemplate.setHashValueSerializer(serializer); return redisTemplate; } /** * 實例化自定義的緩存管理器 * @param redisTemplate * @return */ @Bean @SuppressWarnings(value = {"unchecked", "rawtypes"}) public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) { RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(Objects.requireNonNull(redisTemplate.getConnectionFactory())); RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer())); return new CustomRedisCacheManager(redisCacheWriter, redisCacheConfiguration); } }
到此這篇關(guān)于Spring @Cacheable自定義緩存過期時間的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Spring @Cacheable緩存過期時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中Scanner類與BufferReader類的不同點(非常詳細)
這篇文章主要介紹了Java中Scanner類與BufferReader類的不同點(非常詳細)的相關(guān)資料,需要的朋友可以參考下2016-08-08java如何通過FileOutputStream字節(jié)流向文件中寫數(shù)據(jù)
這篇文章主要介紹了java如何通過FileOutputStream字節(jié)流向文件中寫數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12Maven打包SpringBoot工程的實現(xiàn)示例
在使用Spring Boot和Maven的項目中,你可以使用Maven來打包你的項目,本文主要介紹了Maven打包SpringBoot工程的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-05-05Java結(jié)構(gòu)型設(shè)計模式之裝飾模式詳解
裝飾模式(Decorator Pattern)允許向一個現(xiàn)有的對象添加新的功能,同時又不改變其結(jié)構(gòu)。這種類型的設(shè)計模式屬于結(jié)構(gòu)型模式,它是作為現(xiàn)有類的一個包裝。這種模式創(chuàng)建了一個裝飾類,用來包裝原有的類,并在保持類方法簽名完整性的前提下,提供了額外的功能2023-03-03