SpringBoot使用@Cacheable時設(shè)置部分緩存的過期時間方式
使用@Cacheable時設(shè)置部分緩存的過期時間
業(yè)務(wù)場景
Spring Boot項目中有一些查詢數(shù)據(jù)需要緩存到Redis中,其中有一些緩存是固定數(shù)據(jù)不會改變,那么就沒必要設(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注解對查詢數(shù)據(jù)進行緩存時,使用cacheNames屬性指定了緩存名稱。下面我們就針對不同的cacheNames來設(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的緩存過期時間為6分鐘。
@Cacheable自定義緩存過期時間
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
:緩存默認不過期,所以這里返回自定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默認的序列化方式 * @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中包含'#‘號,則'#'后為緩存生存時間,不存在則表示緩存不進行生存時間設(shè)置
//cacheConfig.entryTtl 設(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ù)時間 //這里默認是以分鐘為單位,所以調(diào)用ofMinutes靜態(tài)方法進行轉(zhuǎn)換 public static Duration ofMinutes(long minutes) { return create(Math.multiplyExact(minutes, SECONDS_PER_MINUTE), 0); }
public class CustomRedisCacheManager extends RedisCacheManager { /* * @description 提供默認構(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) { //名稱中存在#標記進行到期時間配置 if (!name.isEmpty() && name.contains("#")) { String[] SPEL = name.split("#"); if (StringUtils.isNumeric(SPEL[1])) { //配置緩存到期時間 int cycle = Integer.parseInt(SPEL[1]); return super.createRedisCache(SPEL[0], cacheConfig.entryTtl(Duration.ofMinutes(cycle * 24 * 60))); } } return super.createRedisCache(name, cacheConfig); } }
使用
生存時間1天
@Cacheable(value = "cacheTest#1") public String cacheTest() { return "cacheTest"; }
緩存持久,無過期時間
@Cacheable(value = "cacheTest") public String cacheTest() { return "cacheTest"; }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用JNDI連接數(shù)據(jù)庫的實現(xiàn)方法
本文主要介紹了Java使用JNDI連接數(shù)據(jù)庫的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12Java數(shù)據(jù)結(jié)構(gòu)之線段樹的原理與實現(xiàn)
線段樹是一種二叉搜索樹,是用來維護區(qū)間信息的數(shù)據(jù)結(jié)構(gòu)。本文將利用示例詳細講講Java數(shù)據(jù)結(jié)構(gòu)中線段樹的原理與實現(xiàn),需要的可以參考一下2022-06-06通過pipeline配置sonar自動化實現(xiàn)過程解析
這篇文章主要介紹了通過pipeline配置sonar自動化實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下2020-11-11SpringBoot中的Spring Cloud Hystrix原理和用法詳解
在Spring Cloud中,Hystrix是一個非常重要的組件,Hystrix可以幫助我們構(gòu)建具有韌性的分布式系統(tǒng),保證系統(tǒng)的可用性和穩(wěn)定性,在本文中,我們將介紹SpringBoot中的Hystrix,包括其原理和如何使用,需要的朋友可以參考下2023-07-07