Spring @Cacheable自定義緩存過期時間的實現(xiàn)示例
更新時間:2024年05月27日 11:56:27 作者:路漫修遠 上下求索
本文主要介紹了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. 如果沒有"#"則默認不設置過期時間
* 2. 拼接的第一個"#"后面為過期時間,第二個"#"后面為時間單位
* 3. 時間單位的表示使用: d(天)、h(小時)、m(分鐘)、s(秒), 默認為h(小時)
* @param cacheConfig
* @return
*/
@Override
protected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) {
// 解析name,設置過期時間
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ù)"#"后第二位字符判斷過期時間的單位,設置相應的過期時間,默認時間單位是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
* 設置Redis序列化方式,默認使用的是JDKSerializer的序列化方式,效率低,所以這里設置使用FastJsonRedisSerializer
* @param connectionFactory
* @return
*/
@Bean
@SuppressWarnings(value = {"unchecked", "rawtypes"})
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// 設置redis連接(LettuceConnectionFactory實現(xiàn)了RedisConnectionFactory)
redisTemplate.setConnectionFactory(connectionFactory);
FastJsonRedisSerializer serializer = new FastJsonRedisSerializer(Object.class);
// key設置StringRedisSerializer序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
// value設置FastJsonRedisSerializer序列化
redisTemplate.setValueSerializer(serializer);
// Hash key設置序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
// Hash value設置序列化
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);
}
}到此這篇關于Spring @Cacheable自定義緩存過期時間的實現(xiàn)示例的文章就介紹到這了,更多相關Spring @Cacheable緩存過期時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot整合Echarts繪制靜態(tài)數(shù)據(jù)柱狀圖和餅圖
這篇文章給大家介紹了SpringBoot整合Echarts繪制靜態(tài)數(shù)據(jù)柱狀圖和餅圖,文中通過代碼示例給大家介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下2024-03-03
解決Java壓縮zip異常java.util.zip.ZipException:duplicate entry
這篇文章主要介紹了解決Java壓縮zip異常java.util.zip.ZipException:duplicate entry:問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

