Skip to content

Commit

Permalink
Merge pull request #15 from xiaomingfor/master
Browse files Browse the repository at this point in the history
fix:修改全局默认过期时间(默认时间为0)redisTemplate报错问题;增加全局空置过期时间配置
  • Loading branch information
lltx authored Aug 23, 2022
2 parents 3ae0f39 + 34fa27d commit 487f45f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.pig4cloud.plugin</groupId>
<artifactId>multilevel-cache-spring-boot-starter</artifactId>
<version>0.0.7</version>
<version>0.0.8</version>
<name>multilevel-cache-spring-boot-starter</name>
<description>support L1 caffeine and L2 redis cache</description>
<url>https://pig4cloud.com</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public class RedisConfigProp {
*/
private Duration defaultExpiration = Duration.ZERO;

/**
* 全局空值过期时间,默认和有值的过期时间一致,一般设置空值过期时间较短
*/
private Duration defaultNullValuesExpiration = null;

/**
* 每个cacheName的过期时间,优先级比defaultExpiration高
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class RedisCaffeineCache extends AbstractValueAdaptingCache {

private final Duration defaultExpiration;

private final Duration defaultNullValuesExpiration;

private final Map<String, Duration> expires;

private final String topic;
Expand All @@ -50,6 +52,7 @@ public RedisCaffeineCache(String name, RedisTemplate<Object, Object> stringKeyRe
this.caffeineCache = caffeineCache;
this.cachePrefix = cacheConfigProperties.getCachePrefix();
this.defaultExpiration = cacheConfigProperties.getRedis().getDefaultExpiration();
this.defaultNullValuesExpiration = cacheConfigProperties.getRedis().getDefaultNullValuesExpiration();
this.expires = cacheConfigProperties.getRedis().getExpires();
this.topic = cacheConfigProperties.getRedis().getTopic();
}
Expand Down Expand Up @@ -115,9 +118,9 @@ public ValueWrapper putIfAbsent(Object key, Object value) {
}

private void doPut(Object key, Object value) {
Duration expire = getExpire();
Duration expire = getExpire(value);
value = toStoreValue(value);
if (!expire.isNegative()) {
if (!expire.isNegative() && !expire.isZero()) {
stringKeyRedisTemplate.opsForValue().set(getKey(key), value, expire);
}
else {
Expand Down Expand Up @@ -178,9 +181,15 @@ private Object getKey(Object key) {
StringUtils.isEmpty(cachePrefix) ? key.toString() : cachePrefix.concat(":").concat(key.toString()));
}

private Duration getExpire() {
private Duration getExpire(Object value) {
Duration cacheNameExpire = expires.get(this.name);
return cacheNameExpire == null ? defaultExpiration : cacheNameExpire;
if (cacheNameExpire == null) {
cacheNameExpire = defaultExpiration;
}
if (value == null && this.defaultNullValuesExpiration != null) {
cacheNameExpire = this.defaultNullValuesExpiration;
}
return cacheNameExpire;
}

/**
Expand Down

0 comments on commit 487f45f

Please sign in to comment.