mirror of
				https://github.com/YunaiV/ruoyi-vue-pro.git
				synced 2025-10-31 10:37:41 +08:00 
			
		
		
		
	!562 优化 redisCache
This commit is contained in:
		| @ -7,15 +7,21 @@ import org.springframework.validation.annotation.Validated; | |||||||
| /** | /** | ||||||
|  * Cache 配置项 |  * Cache 配置项 | ||||||
|  * |  * | ||||||
|  * @author |  * @author Wanwan | ||||||
|  */ |  */ | ||||||
| @ConfigurationProperties("yudao.cache") | @ConfigurationProperties("yudao.cache") | ||||||
| @Data | @Data | ||||||
| @Validated | @Validated | ||||||
| public class YudaoCacheProperties { | public class YudaoCacheProperties { | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * {@link #redisScanBatchSize} 默认值 | ||||||
|  |      */ | ||||||
|  |     private static final Integer REDIS_SCAN_BATCH_SIZE_DEFAULT = 30; | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * redis scan 一次返回数量 |      * redis scan 一次返回数量 | ||||||
|      */ |      */ | ||||||
|     private Integer redisScanBatchSize = 30; |     private Integer redisScanBatchSize = REDIS_SCAN_BATCH_SIZE_DEFAULT; | ||||||
|  |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -2,7 +2,6 @@ package cn.iocoder.yudao.framework.redis.core; | |||||||
|  |  | ||||||
| import cn.hutool.core.util.NumberUtil; | import cn.hutool.core.util.NumberUtil; | ||||||
| import cn.hutool.core.util.StrUtil; | import cn.hutool.core.util.StrUtil; | ||||||
| import org.springframework.boot.convert.DurationStyle; |  | ||||||
| import org.springframework.cache.annotation.Cacheable; | import org.springframework.cache.annotation.Cacheable; | ||||||
| import org.springframework.data.redis.cache.RedisCache; | import org.springframework.data.redis.cache.RedisCache; | ||||||
| import org.springframework.data.redis.cache.RedisCacheConfiguration; | import org.springframework.data.redis.cache.RedisCacheConfiguration; | ||||||
| @ -10,12 +9,12 @@ import org.springframework.data.redis.cache.RedisCacheManager; | |||||||
| import org.springframework.data.redis.cache.RedisCacheWriter; | import org.springframework.data.redis.cache.RedisCacheWriter; | ||||||
|  |  | ||||||
| import java.time.Duration; | import java.time.Duration; | ||||||
| import java.time.temporal.ChronoUnit; |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * 支持自定义过期时间的 {@link RedisCacheManager} 实现类 |  * 支持自定义过期时间的 {@link RedisCacheManager} 实现类 | ||||||
|  * <p> |  * | ||||||
|  * 在 {@link Cacheable#cacheNames()} 格式为 "key#ttl" 时,# 后面的 ttl 为过期时间,单位为最后一个字母(支持的单位有:d天,h小时,m分钟,s秒),默认单位为秒 |  * 在 {@link Cacheable#cacheNames()} 格式为 "key#ttl" 时,# 后面的 ttl 为过期时间。 | ||||||
|  |  * 单位为最后一个字母(支持的单位有:d 天,h 小时,m 分钟,s 秒),默认单位为 s 秒 | ||||||
|  * |  * | ||||||
|  * @author 芋道源码 |  * @author 芋道源码 | ||||||
|  */ |  */ | ||||||
| @ -50,34 +49,35 @@ public class TimeoutRedisCacheManager extends RedisCacheManager { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 解析 Duration |      * 解析过期时间 Duration | ||||||
|      * |      * | ||||||
|      * @param ttlStr |      * @param ttlStr 过期时间字符串 | ||||||
|      * @return |      * @return 过期时间 Duration | ||||||
|      */ |      */ | ||||||
|     private Duration parseDuration(String ttlStr) { |     private Duration parseDuration(String ttlStr) { | ||||||
|         String timeUnit = StrUtil.subSuf(ttlStr, -1); |         String timeUnit = StrUtil.subSuf(ttlStr, -1); | ||||||
|         switch (timeUnit) { |         switch (timeUnit) { | ||||||
|             case "d": |             case "d": | ||||||
|                 return Duration.ofDays(removeSuffix(ttlStr)); |                 return Duration.ofDays(removeDurationSuffix(ttlStr)); | ||||||
|             case "h": |             case "h": | ||||||
|                 return Duration.ofHours(removeSuffix(ttlStr)); |                 return Duration.ofHours(removeDurationSuffix(ttlStr)); | ||||||
|             case "m": |             case "m": | ||||||
|                 return Duration.ofMinutes(removeSuffix(ttlStr)); |                 return Duration.ofMinutes(removeDurationSuffix(ttlStr)); | ||||||
|             case "s": |             case "s": | ||||||
|                 return Duration.ofSeconds(removeSuffix(ttlStr)); |                 return Duration.ofSeconds(removeDurationSuffix(ttlStr)); | ||||||
|             default: |             default: | ||||||
|                 return Duration.ofSeconds(Long.parseLong(ttlStr)); |                 return Duration.ofSeconds(Long.parseLong(ttlStr)); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 移除多余的后缀 |      * 移除多余的后缀,返回具体的时间 | ||||||
|      * |      * | ||||||
|      * @param ttlStr |      * @param ttlStr 过期时间字符串 | ||||||
|      * @return |      * @return 时间 | ||||||
|      */ |      */ | ||||||
|     private Long removeSuffix(String ttlStr) { |     private Long removeDurationSuffix(String ttlStr) { | ||||||
|         return NumberUtil.parseLong(StrUtil.sub(ttlStr, 0, ttlStr.length() - 1)); |         return NumberUtil.parseLong(StrUtil.sub(ttlStr, 0, ttlStr.length() - 1)); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -204,8 +204,6 @@ yudao: | |||||||
|     order: |     order: | ||||||
|       app-id: 1 # 商户编号 |       app-id: 1 # 商户编号 | ||||||
|       expire-time: 2h # 支付的过期时间 |       expire-time: 2h # 支付的过期时间 | ||||||
|   cache: # spring cache 相关配置 |  | ||||||
|     redis-scan-batch-size: 30 # redis scan 一次返回数量 |  | ||||||
|  |  | ||||||
| debug: false | debug: false | ||||||
|  |  | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV