From d0c4b5bc76eed132a1d2028d205078866fc346fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Tue, 23 Nov 2021 10:20:44 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E5=A2=9E=E5=8A=A0=20RedisUtils=20set=20?= =?UTF-8?q?=E4=BF=9D=E7=95=99=20TTL=20=E6=9C=89=E6=95=88=E6=9C=9F=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/utils/RedisUtils.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java index 394bdb29..89206c97 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java @@ -88,7 +88,24 @@ public class RedisUtils { * @param value 缓存的值 */ public static void setCacheObject(final String key, final T value) { - client.getBucket(key).set(value); + setCacheObject(key, value, false); + } + + /** + * 缓存基本的对象,保留当前对象 TTL 有效期 + * + * @param key 缓存的键值 + * @param value 缓存的值 + * @param isSaveTtl 是否保留TTL有效期(例如: set之前ttl剩余90 set之后还是为90) + * @since Redis 6.0.0 以上有效 + */ + public static void setCacheObject(final String key, final T value, final boolean isSaveTtl) { + RBucket bucket = client.getBucket(key); + if (isSaveTtl) { + bucket.setAndKeepTTL(value); + } else { + bucket.set(value); + } } /**