Commit 9ff67b479fd09fe500aa8ebdee57d0784aea0215
1 parent
8960a829
!562 优化 redisCache
Showing
3 changed files
with
23 additions
and
19 deletions
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 | /** |
| 8 | 8 | * Cache 配置项 |
| 9 | 9 | * |
| 10 | - * @author | |
| 10 | + * @author Wanwan | |
| 11 | 11 | */ |
| 12 | 12 | @ConfigurationProperties("yudao.cache") |
| 13 | 13 | @Data |
| ... | ... | @@ -15,7 +15,13 @@ import org.springframework.validation.annotation.Validated; |
| 15 | 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 | 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 | 2 | |
| 3 | 3 | import cn.hutool.core.util.NumberUtil; |
| 4 | 4 | import cn.hutool.core.util.StrUtil; |
| 5 | -import org.springframework.boot.convert.DurationStyle; | |
| 6 | 5 | import org.springframework.cache.annotation.Cacheable; |
| 7 | 6 | import org.springframework.data.redis.cache.RedisCache; |
| 8 | 7 | import org.springframework.data.redis.cache.RedisCacheConfiguration; |
| ... | ... | @@ -10,12 +9,12 @@ import org.springframework.data.redis.cache.RedisCacheManager; |
| 10 | 9 | import org.springframework.data.redis.cache.RedisCacheWriter; |
| 11 | 10 | |
| 12 | 11 | import java.time.Duration; |
| 13 | -import java.time.temporal.ChronoUnit; | |
| 14 | 12 | |
| 15 | 13 | /** |
| 16 | 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 | 19 | * @author 芋道源码 |
| 21 | 20 | */ |
| ... | ... | @@ -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 | 57 | private Duration parseDuration(String ttlStr) { |
| 59 | 58 | String timeUnit = StrUtil.subSuf(ttlStr, -1); |
| 60 | 59 | switch (timeUnit) { |
| 61 | 60 | case "d": |
| 62 | - return Duration.ofDays(removeSuffix(ttlStr)); | |
| 61 | + return Duration.ofDays(removeDurationSuffix(ttlStr)); | |
| 63 | 62 | case "h": |
| 64 | - return Duration.ofHours(removeSuffix(ttlStr)); | |
| 63 | + return Duration.ofHours(removeDurationSuffix(ttlStr)); | |
| 65 | 64 | case "m": |
| 66 | - return Duration.ofMinutes(removeSuffix(ttlStr)); | |
| 65 | + return Duration.ofMinutes(removeDurationSuffix(ttlStr)); | |
| 67 | 66 | case "s": |
| 68 | - return Duration.ofSeconds(removeSuffix(ttlStr)); | |
| 67 | + return Duration.ofSeconds(removeDurationSuffix(ttlStr)); | |
| 69 | 68 | default: |
| 70 | 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 | 80 | return NumberUtil.parseLong(StrUtil.sub(ttlStr, 0, ttlStr.length() - 1)); |
| 82 | 81 | } |
| 82 | + | |
| 83 | 83 | } | ... | ... |
yudao-server/src/main/resources/application.yaml