Remember to configure the expiration time of @Cacheable (Redis specifies the expiration time of certain Cache Keys)

OK_boom 2022-08-06 17:28:01 阅读数:555

rememberconfigureexpirationtimecacheable

All configurations are being generatedRedisCacheManagershould have been specified before(在Builder里面),The following code is specifiedBuilderThe default configuration and specify certainkey的配置的 代码:

// 配置序列化(解决乱码的问题),And configure the cache default validity period 6小时
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(6));
RedisCacheConfiguration redisCacheConfiguration = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));
...
RedisCacheManager.RedisCacheManagerBuilder builder=RedisCacheManager.builder(writer).cacheDefaults(redisCacheConfiguration)
.withInitialCacheConfigurations(singletonMap(CacheConstant.SYS_DICT_TABLE_CACHE,
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10)).disableCachingNullValues()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRe
disSerializer))));
...
//Access custom configuration
for (String v:keyExpire){

String[] items=v.split(",");
if (items.length!=2){

throw new IllegalArgumentException("Invalid configuration parameter:jeecg.redis.key-expire:"+v);
}
builder.withInitialCacheConfigurations(singletonMap(items[0],
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(Long.valueOf(items[1]))).disableCachingNullValues()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))));
}
RedisCacheManager cacheManager=builder.transactionAware().build();
return cacheManager;

在applicationdefine somecache keyThe expiration time is the same as in the code abovekeyExpire对接

 redis:
#Custom cache key validity period,秒
key-expire:
- smp:order:status:list,300

@Cacheable调用:

@Override
@Cacheable(value = CACHE_STATUS_LIST,key="#tenantId+'-'+T(com.freestyle.common.CacheIdUtil).toId(#queryWrapper,#queryWrapper)")
public IPage<SmpOrderStatus> page(String tenantId, Page<SmpOrderStatus> page, QueryWrapper<SmpOrderStatus> queryWrapper) {

return super.page(page,queryWrapper);
}
copyright:author[OK_boom],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/218/202208061704291619.html