From 42295ef2ac0a5e78674cf24b62d6834138f0ffdc Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期一, 29 十一月 2021 13:56:25 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/dev' into satoken --- ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 43 insertions(+), 3 deletions(-) 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 89206c9..04db787 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 @@ -97,12 +97,41 @@ * @param key 缂撳瓨鐨勯敭鍊� * @param value 缂撳瓨鐨勫�� * @param isSaveTtl 鏄惁淇濈暀TTL鏈夋晥鏈�(渚嬪: set涔嬪墠ttl鍓╀綑90 set涔嬪悗杩樻槸涓�90) - * @since Redis 6.0.0 浠ヤ笂鏈夋晥 + * @since Redis 6.X 浠ヤ笂浣跨敤 setAndKeepTTL 鍏煎 5.X 鏂规 */ public static <T> void setCacheObject(final String key, final T value, final boolean isSaveTtl) { RBucket<Object> bucket = client.getBucket(key); if (isSaveTtl) { - bucket.setAndKeepTTL(value); + try { + bucket.setAndKeepTTL(value); + } catch (Exception e) { + long timeToLive = bucket.remainTimeToLive(); + bucket.set(value); + bucket.expire(timeToLive, TimeUnit.MILLISECONDS); + } + } else { + bucket.set(value); + } + } + + /** + * 缂撳瓨鍩烘湰鐨勫璞★紝淇濈暀褰撳墠瀵硅薄 TTL 鏈夋晥鏈� + * + * @param key 缂撳瓨鐨勯敭鍊� + * @param value 缂撳瓨鐨勫�� + * @param isSaveTtl 鏄惁淇濈暀TTL鏈夋晥鏈�(渚嬪: set涔嬪墠ttl鍓╀綑90 set涔嬪悗杩樻槸涓�90) + * @since Redis 6.X 浠ヤ笂浣跨敤 setAndKeepTTL 鍏煎 5.X 鏂规 + */ + public static <T> void setCacheObject(final String key, final T value, final boolean isSaveTtl) { + RBucket<Object> bucket = client.getBucket(key); + if (isSaveTtl) { + try { + bucket.setAndKeepTTL(value); + } catch (Exception e) { + long timeToLive = bucket.remainTimeToLive(); + bucket.set(value); + bucket.expire(timeToLive, TimeUnit.MILLISECONDS); + } } else { bucket.set(value); } @@ -116,7 +145,7 @@ * @param timeout 鏃堕棿 * @param timeUnit 鏃堕棿棰楃矑搴� */ - public static <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) { + public static <T> void setCacheObject(final String key, final T value, final long timeout, final TimeUnit timeUnit) { RBucket<T> result = client.getBucket(key); result.set(value); result.expire(timeout, timeUnit); @@ -158,6 +187,17 @@ } /** + * 鑾峰緱key鍓╀綑瀛樻椿鏃堕棿 + * + * @param key 缂撳瓨閿�� + * @return 鍓╀綑瀛樻椿鏃堕棿 + */ + public static <T> long getTimeToLive(final String key) { + RBucket<T> rBucket = client.getBucket(key); + return rBucket.remainTimeToLive(); + } + + /** * 鍒犻櫎鍗曚釜瀵硅薄 * * @param key -- Gitblit v1.9.3