SpringBoot使用@Cacheable時(shí)設(shè)置部分緩存的過期時(shí)間方式
使用@Cacheable時(shí)設(shè)置部分緩存的過期時(shí)間
業(yè)務(wù)場景
Spring Boot項(xiàng)目中有一些查詢數(shù)據(jù)需要緩存到Redis中,其中有一些緩存是固定數(shù)據(jù)不會(huì)改變,那么就沒必要設(shè)置過期時(shí)間。還有一些緩存需要每隔幾分鐘就更新一次,這時(shí)就需要設(shè)置過期時(shí)間。
Service層部分代碼如下:
@Override
@Cacheable(cacheNames = {"distributor"}, key = "#root.methodName")
public List<CityVO> findCities() {
return distributorMapper.selectCities();
}
@Override
@Cacheable(cacheNames = {"distributor"}, key = "#root.methodName.concat('#cityId').concat(#cityId)")
public List<DistributorVO> findDistributorsByCityId(String cityId) {
return distributorMapper.selectByCityId(cityId);
}
@Override
@Cacheable(cacheNames = {"car"}, key = "#root.methodName.concat('#cityId').concat(#cityId)")
public String carList(String cityId) {
RequestData data = new RequestData();
data.setCityId(cityId);
CarListParam param = new CarListParam();
param.setRequestData(data);
String jsonParam = JSON.toJSONString(param);
return HttpClientUtil.sendPostWithJson(ApiUrlConst.MULE_APP, jsonParam);
}
在使用@Cacheable注解對(duì)查詢數(shù)據(jù)進(jìn)行緩存時(shí),使用cacheNames屬性指定了緩存名稱。下面我們就針對(duì)不同的cacheNames來設(shè)置失效時(shí)間。
添加Redis配置類RedisConfig.java
代碼如下:
@Slf4j
@Configuration
@EnableCaching //啟用緩存
public class RedisConfig {
/**
* 自定義緩存管理器
*/
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory factory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
Set<String> cacheNames = new HashSet<>();
cacheNames.add("car");
cacheNames.add("distributor");
ConcurrentHashMap<String, RedisCacheConfiguration> configMap = new ConcurrentHashMap<>();
configMap.put("car", config.entryTtl(Duration.ofMinutes(6L)));
configMap.put("distributor", config);
//需要先初始化緩存名稱,再初始化其它的配置。
RedisCacheManager cacheManager = RedisCacheManager.builder(factory).initialCacheNames(cacheNames).withInitialCacheConfigurations(configMap).build();
return cacheManager;
}
}
上面代碼,在configMap中指定了cacheNames為car的緩存過期時(shí)間為6分鐘。
@Cacheable自定義緩存過期時(shí)間
pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
yml
# redis配置
spring:
redis:
database: 0
host: 127.0.0.1
password: 123456
port: 6379
timeout: 5000
lettuce:
pool:
max-active: 300
max-wait: -1
max-idle: 20
min-idle: 10
RedisConfig
RedisCacheManager:緩存默認(rèn)不過期,所以這里返回自定RedisCacheManager
//return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration); return new CustomRedisCacheManager(redisCacheWriter, redisCacheConfiguration);
@Configuration
public class RedisConfig {
/*
* @description redis序列化方式
* @author xianping
* @date 2020/9/25
* @param redisConnectionFactory
* @return RedisTemplate
**/
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
/*
* @description Redis緩存的序列化方式使用redisTemplate.getValueSerializer(),不在使用JDK默認(rèn)的序列化方式
* @author xianping
* @date 2020/9/25
* @param redisTemplate
* @return RedisCacheManager
**/
@Bean
public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()));
//return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
return new CustomRedisCacheManager(redisCacheWriter, redisCacheConfiguration);
}
}
CustomRedisCacheManager
自定義RedisCacheManager:若@Cacheable value中包含'#‘號(hào),則'#'后為緩存生存時(shí)間,不存在則表示緩存不進(jìn)行生存時(shí)間設(shè)置
//cacheConfig.entryTtl 設(shè)置緩存過期時(shí)間
public RedisCacheConfiguration entryTtl(Duration ttl) {
Assert.notNull(ttl, "TTL duration must not be null!");
return new RedisCacheConfiguration(ttl, this.cacheNullValues, this.usePrefix, this.keyPrefix, this.keySerializationPair, this.valueSerializationPair, this.conversionService);
}
//Duration.ofMinutes 持續(xù)時(shí)間
//這里默認(rèn)是以分鐘為單位,所以調(diào)用ofMinutes靜態(tài)方法進(jìn)行轉(zhuǎn)換
public static Duration ofMinutes(long minutes) {
return create(Math.multiplyExact(minutes, SECONDS_PER_MINUTE), 0);
}
public class CustomRedisCacheManager extends RedisCacheManager {
/*
* @description 提供默認(rèn)構(gòu)造器
* @author xianping
* @date 2020/9/28 9:22
* @param
* @param cacheWriter
* @param defaultCacheConfiguration
* @return
**/
public CustomRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
super(cacheWriter, defaultCacheConfiguration);
}
/*
* @description 重寫父類createRedisCache方法
* @author xianping
* @date 2020/9/28 9:22
* @param
* @param name @Cacheable中的value
* @param cacheConfig
* @return org.springframework.data.redis.cache.RedisCache
**/
@Override
protected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) {
//名稱中存在#標(biāo)記進(jìn)行到期時(shí)間配置
if (!name.isEmpty() && name.contains("#")) {
String[] SPEL = name.split("#");
if (StringUtils.isNumeric(SPEL[1])) {
//配置緩存到期時(shí)間
int cycle = Integer.parseInt(SPEL[1]);
return super.createRedisCache(SPEL[0], cacheConfig.entryTtl(Duration.ofMinutes(cycle * 24 * 60)));
}
}
return super.createRedisCache(name, cacheConfig);
}
}
使用
生存時(shí)間1天
@Cacheable(value = "cacheTest#1")
public String cacheTest() {
return "cacheTest";
}
緩存持久,無過期時(shí)間
@Cacheable(value = "cacheTest")
public String cacheTest() {
return "cacheTest";
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中的五種對(duì)靜態(tài)資源的映射規(guī)則的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot中的五種對(duì)靜態(tài)資源的映射規(guī)則的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Idea工具中創(chuàng)建 SpringBoot工程及入門詳解
這篇文章主要介紹了Idea工具中創(chuàng)建 SpringBoot工程及入門分析詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Java設(shè)計(jì)模式編程中的責(zé)任鏈模式使用示例
這篇文章主要介紹了Java設(shè)計(jì)模式編程中的責(zé)任鏈模式使用示例,責(zé)任鏈模式可以避免很多請(qǐng)求的發(fā)送者和接收者之間的耦合關(guān)系,需要的朋友可以參考下2016-05-05
基于SpringBoot加載Mybatis的TypeAlias問題
這篇文章主要介紹了解決SpringBoot加載Mybatis的TypeAlias問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot+@EnableScheduling使用定時(shí)器的常見案例
項(xiàng)目開發(fā)中經(jīng)常需要執(zhí)行一些定時(shí)任務(wù),本文主要介紹了SpringBoot+@EnableScheduling使用定時(shí)器的常見案例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09

