Commit 9ff67b479fd09fe500aa8ebdee57d0784aea0215

Authored by YunaiV
1 parent 8960a829

!562 优化 redisCache

yudao-framework/yudao-spring-boot-starter-redis/src/main/java/cn/iocoder/yudao/framework/redis/config/YudaoCacheProperties.java
@@ -7,7 +7,7 @@ import org.springframework.validation.annotation.Validated; @@ -7,7 +7,7 @@ import org.springframework.validation.annotation.Validated;
7 /** 7 /**
8 * Cache 配置项 8 * Cache 配置项
9 * 9 *
10 - * @author 10 + * @author Wanwan
11 */ 11 */
12 @ConfigurationProperties("yudao.cache") 12 @ConfigurationProperties("yudao.cache")
13 @Data 13 @Data
@@ -15,7 +15,13 @@ import org.springframework.validation.annotation.Validated; @@ -15,7 +15,13 @@ import org.springframework.validation.annotation.Validated;
15 public class YudaoCacheProperties { 15 public class YudaoCacheProperties {
16 16
17 /** 17 /**
  18 + * {@link #redisScanBatchSize} 默认值
  19 + */
  20 + private static final Integer REDIS_SCAN_BATCH_SIZE_DEFAULT = 30;
  21 +
  22 + /**
18 * redis scan 一次返回数量 23 * redis scan 一次返回数量
19 */ 24 */
20 - private Integer redisScanBatchSize = 30; 25 + private Integer redisScanBatchSize = REDIS_SCAN_BATCH_SIZE_DEFAULT;
  26 +
21 } 27 }
yudao-framework/yudao-spring-boot-starter-redis/src/main/java/cn/iocoder/yudao/framework/redis/core/TimeoutRedisCacheManager.java
@@ -2,7 +2,6 @@ package cn.iocoder.yudao.framework.redis.core; @@ -2,7 +2,6 @@ package cn.iocoder.yudao.framework.redis.core;
2 2
3 import cn.hutool.core.util.NumberUtil; 3 import cn.hutool.core.util.NumberUtil;
4 import cn.hutool.core.util.StrUtil; 4 import cn.hutool.core.util.StrUtil;
5 -import org.springframework.boot.convert.DurationStyle;  
6 import org.springframework.cache.annotation.Cacheable; 5 import org.springframework.cache.annotation.Cacheable;
7 import org.springframework.data.redis.cache.RedisCache; 6 import org.springframework.data.redis.cache.RedisCache;
8 import org.springframework.data.redis.cache.RedisCacheConfiguration; 7 import org.springframework.data.redis.cache.RedisCacheConfiguration;
@@ -10,12 +9,12 @@ import org.springframework.data.redis.cache.RedisCacheManager; @@ -10,12 +9,12 @@ import org.springframework.data.redis.cache.RedisCacheManager;
10 import org.springframework.data.redis.cache.RedisCacheWriter; 9 import org.springframework.data.redis.cache.RedisCacheWriter;
11 10
12 import java.time.Duration; 11 import java.time.Duration;
13 -import java.time.temporal.ChronoUnit;  
14 12
15 /** 13 /**
16 * 支持自定义过期时间的 {@link RedisCacheManager} 实现类 14 * 支持自定义过期时间的 {@link RedisCacheManager} 实现类
17 - * <p>  
18 - * 在 {@link Cacheable#cacheNames()} 格式为 "key#ttl" 时,# 后面的 ttl 为过期时间,单位为最后一个字母(支持的单位有:d天,h小时,m分钟,s秒),默认单位为秒 15 + *
  16 + * 在 {@link Cacheable#cacheNames()} 格式为 "key#ttl" 时,# 后面的 ttl 为过期时间。
  17 + * 单位为最后一个字母(支持的单位有:d 天,h 小时,m 分钟,s 秒),默认单位为 s 秒
19 * 18 *
20 * @author 芋道源码 19 * @author 芋道源码
21 */ 20 */
@@ -50,34 +49,35 @@ public class TimeoutRedisCacheManager extends RedisCacheManager { @@ -50,34 +49,35 @@ public class TimeoutRedisCacheManager extends RedisCacheManager {
50 } 49 }
51 50
52 /** 51 /**
53 - * 解析 Duration 52 + * 解析过期时间 Duration
54 * 53 *
55 - * @param ttlStr  
56 - * @return 54 + * @param ttlStr 过期时间字符串
  55 + * @return 过期时间 Duration
57 */ 56 */
58 private Duration parseDuration(String ttlStr) { 57 private Duration parseDuration(String ttlStr) {
59 String timeUnit = StrUtil.subSuf(ttlStr, -1); 58 String timeUnit = StrUtil.subSuf(ttlStr, -1);
60 switch (timeUnit) { 59 switch (timeUnit) {
61 case "d": 60 case "d":
62 - return Duration.ofDays(removeSuffix(ttlStr)); 61 + return Duration.ofDays(removeDurationSuffix(ttlStr));
63 case "h": 62 case "h":
64 - return Duration.ofHours(removeSuffix(ttlStr)); 63 + return Duration.ofHours(removeDurationSuffix(ttlStr));
65 case "m": 64 case "m":
66 - return Duration.ofMinutes(removeSuffix(ttlStr)); 65 + return Duration.ofMinutes(removeDurationSuffix(ttlStr));
67 case "s": 66 case "s":
68 - return Duration.ofSeconds(removeSuffix(ttlStr)); 67 + return Duration.ofSeconds(removeDurationSuffix(ttlStr));
69 default: 68 default:
70 return Duration.ofSeconds(Long.parseLong(ttlStr)); 69 return Duration.ofSeconds(Long.parseLong(ttlStr));
71 } 70 }
72 } 71 }
73 72
74 /** 73 /**
75 - * 移除多余的后缀 74 + * 移除多余的后缀,返回具体的时间
76 * 75 *
77 - * @param ttlStr  
78 - * @return 76 + * @param ttlStr 过期时间字符串
  77 + * @return 时间
79 */ 78 */
80 - private Long removeSuffix(String ttlStr) { 79 + private Long removeDurationSuffix(String ttlStr) {
81 return NumberUtil.parseLong(StrUtil.sub(ttlStr, 0, ttlStr.length() - 1)); 80 return NumberUtil.parseLong(StrUtil.sub(ttlStr, 0, ttlStr.length() - 1));
82 } 81 }
  82 +
83 } 83 }
yudao-server/src/main/resources/application.yaml
@@ -204,8 +204,6 @@ yudao: @@ -204,8 +204,6 @@ yudao:
204 order: 204 order:
205 app-id: 1 # 商户编号 205 app-id: 1 # 商户编号
206 expire-time: 2h # 支付的过期时间 206 expire-time: 2h # 支付的过期时间
207 - cache: # spring cache 相关配置  
208 - redis-scan-batch-size: 30 # redis scan 一次返回数量  
209 207
210 debug: false 208 debug: false
211 209