From e2d370bd9d3d51c8bb4d702addb466fa5f3f13bf Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期五, 26 十一月 2021 13:42:20 +0800 Subject: [PATCH] update 移除 satoken 自带集成 redis 改为使用 框架自身 Redission 实现 统一方案 性能优异 --- ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java | 74 ++++++++++++++++++++++++++++++++++++ 1 files changed, 73 insertions(+), 1 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..27472a7 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 @@ -66,6 +92,29 @@ } /** + * 缂撳瓨鍩烘湰鐨勫璞★紝淇濈暀褰撳墠瀵硅薄 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); + } + } + + /** * 缂撳瓨鍩烘湰鐨勫璞★紝Integer銆丼tring銆佸疄浣撶被绛� * * @param key 缂撳瓨鐨勯敭鍊� @@ -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