feat(pigx-common-data): #749 【新功能】增强 cache 组件 cacheName 时长支持单位

Closes #749
This commit is contained in:
冷冷 committed 2019-11-09 18:50:06 +08:00
1 parent 07c20f36c9
commit c551912488
1 file changed
+6 -3
@@ -21,6 +21,7 @@ import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
import com.pig4cloud.pigx.common.data.tenant.TenantContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.convert.DurationStyle;
import org.springframework.cache.Cache;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
@@ -29,6 +30,7 @@ import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.lang.Nullable;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Map;
/**
@@ -61,21 +63,22 @@ public class RedisAutoCacheManager extends RedisCacheManager {
}
if (cacheConfig != null) {
long cacheAge = Long.parseLong(cacheArray[1]);
cacheConfig = cacheConfig.entryTtl(Duration.ofSeconds(cacheAge));
Duration duration = DurationStyle.detectAndParse(cacheArray[1], ChronoUnit.SECONDS);
cacheConfig = cacheConfig.entryTtl(duration);
}
return super.createRedisCache(name, cacheConfig);
}
/**
* 从上下文中获取租户ID,重写@Cacheable value 值
*
* @param name
* @return
*/
@Override
public Cache getCache(String name) {
// see https://gitee.wang/pig/pigx/issues/613
if(name.startsWith(CacheConstants.GLOBALLY)){
if (name.startsWith(CacheConstants.GLOBALLY)) {
return super.getCache(name);
}
return super.getCache(TenantContextHolder.getTenantId() + StrUtil.COLON + name);