From 1010ccca4dfa63f68f3caa87ddaadcfbe33405a5 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期五, 26 十一月 2021 13:34:18 +0800 Subject: [PATCH] update 更新 RedisUtils set保留ttl 兼容 5.X redis版本 --- ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java | 76 +++++++++++++++++++++++++++++++++++++- 1 files changed, 74 insertions(+), 2 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 5fcfb9d..7f4d2ca 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 @@ -26,6 +26,32 @@ private static RedissonClient client = SpringUtils.getBean(RedissonClient.class); /** + * 闄愭祦 + * + * @param key 闄愭祦key + * @param rateType 闄愭祦绫诲瀷 + * @param rate 閫熺巼 + * @param rateInterval 閫熺巼闂撮殧 + * @return -1 琛ㄧず澶辫触 + */ + public static long rateLimiter(String key, RateType rateType, int rate, int rateInterval) { + RRateLimiter rateLimiter = client.getRateLimiter(key); + rateLimiter.trySetRate(rateType, rate, rateInterval, RateIntervalUnit.SECONDS); + if (rateLimiter.tryAcquire()) { + return rateLimiter.availablePermits(); + } else { + return -1L; + } + } + + /** + * 鑾峰彇瀹炰緥id + */ + public static String getClientId() { + return client.getId(); + } + + /** * 鍙戝竷閫氶亾娑堟伅 * * @param channelKey 閫氶亾key @@ -62,7 +88,30 @@ * @param value 缂撳瓨鐨勫�� */ public static <T> 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.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); + } } /** @@ -73,7 +122,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); @@ -112,6 +161,17 @@ public static <T> T getCacheObject(final String key) { RBucket<T> rBucket = client.getBucket(key); return rBucket.get(); + } + + /** + * 鑾峰緱key鍓╀綑瀛樻椿鏃堕棿 + * + * @param key 缂撳瓨閿�� + * @return 鍓╀綑瀛樻椿鏃堕棿 + */ + public static <T> long getTimeToLive(final String key) { + RBucket<T> rBucket = client.getBucket(key); + return rBucket.remainTimeToLive(); } /** @@ -234,6 +294,18 @@ } /** + * 鍒犻櫎Hash涓殑鏁版嵁 + * + * @param key Redis閿� + * @param hKey Hash閿� + * @return Hash涓殑瀵硅薄 + */ + public static <T> T delCacheMapValue(final String key, final String hKey) { + RMap<String, T> rMap = client.getMap(key); + return rMap.remove(hKey); + } + + /** * 鑾峰彇澶氫釜Hash涓殑鏁版嵁 * * @param key Redis閿� -- Gitblit v1.9.3