疯狂的狮子li
2021-11-29 42295ef2ac0a5e78674cf24b62d6834138f0ffdc
ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java
@@ -29,14 +29,26 @@
     * 限流
     *
     * @param key          限流key
     * @param limitType    限流类型
     * @param rateType     限流类型
     * @param rate         速率
     * @param rateInterval 速率间隔
     * @return -1 表示失败
     */
    public static boolean rateLimiter(String key, RateType rateType, int rate, int rateInterval) {
    public static long rateLimiter(String key, RateType rateType, int rate, int rateInterval) {
        RRateLimiter rateLimiter = client.getRateLimiter(key);
        rateLimiter.trySetRate(rateType, rate, rateInterval, RateIntervalUnit.SECONDS);
        return rateLimiter.tryAcquire();
        if (rateLimiter.tryAcquire()) {
            return rateLimiter.availablePermits();
        } else {
            return -1L;
        }
    }
    /**
     * 获取实例id
     */
    public static String getClientId() {
        return client.getId();
    }
    /**
@@ -76,7 +88,53 @@
     * @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);
        }
    }
    /**
     * 缓存基本的对象,保留当前对象 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);
        }
    }
    /**
@@ -87,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);
@@ -126,6 +184,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();
    }
    /**
@@ -248,6 +317,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键