| | |
| | | package com.ruoyi.common.utils.redis; |
| | | |
| | | import cn.hutool.core.collection.IterUtil; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.redisson.api.*; |
| | | import org.redisson.config.Config; |
| | | |
| | | import java.time.Duration; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.function.Consumer; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | | /** |
| | | * redis 工具类 |
| | |
| | | public class RedisUtils { |
| | | |
| | | private static final RedissonClient CLIENT = SpringUtils.getBean(RedissonClient.class); |
| | | |
| | | public static NameMapper getNameMapper() { |
| | | Config config = CLIENT.getConfig(); |
| | | if (config.isClusterConfig()) { |
| | | return config.useClusterServers().getNameMapper(); |
| | | } |
| | | return config.useSingleServer().getNameMapper(); |
| | | } |
| | | |
| | | /** |
| | | * 限流 |
| | |
| | | * @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); |
| | | RBucket<T> 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); |
| | | setCacheObject(key, value, Duration.ofMillis(timeToLive)); |
| | | } |
| | | } else { |
| | | bucket.set(value); |
| | |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param value 缓存的值 |
| | | * @param timeout 时间 |
| | | * @param timeUnit 时间颗粒度 |
| | | * @param duration 时间 |
| | | */ |
| | | 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); |
| | | public static <T> void setCacheObject(final String key, final T value, final Duration duration) { |
| | | RBatch batch = CLIENT.createBatch(); |
| | | RBucketAsync<T> bucket = batch.getBucket(key); |
| | | bucket.setAsync(value); |
| | | bucket.expireAsync(duration); |
| | | batch.execute(); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return true=设置成功;false=设置失败 |
| | | */ |
| | | public static boolean expire(final String key, final long timeout) { |
| | | return expire(key, timeout, TimeUnit.SECONDS); |
| | | return expire(key, Duration.ofSeconds(timeout)); |
| | | } |
| | | |
| | | /** |
| | | * 设置有效时间 |
| | | * |
| | | * @param key Redis键 |
| | | * @param timeout 超时时间 |
| | | * @param unit 时间单位 |
| | | * @param key Redis键 |
| | | * @param duration 超时时间 |
| | | * @return true=设置成功;false=设置失败 |
| | | */ |
| | | public static boolean expire(final String key, final long timeout, final TimeUnit unit) { |
| | | public static boolean expire(final String key, final Duration duration) { |
| | | RBucket rBucket = CLIENT.getBucket(key); |
| | | return rBucket.expire(timeout, unit); |
| | | return rBucket.expire(duration); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获得缓存Map的key列表 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @return key列表 |
| | | */ |
| | | public static <T> Set<String> getCacheMapKeySet(final String key) { |
| | | RMap<String, T> rMap = CLIENT.getMap(key); |
| | | return rMap.keySet(); |
| | | } |
| | | |
| | | /** |
| | | * 往Hash中存入数据 |
| | | * |
| | | * @param key Redis键 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 设置原子值 |
| | | * |
| | | * @param key Redis键 |
| | | * @param value 值 |
| | | */ |
| | | public static void setAtomicValue(String key, long value) { |
| | | RAtomicLong atomic = CLIENT.getAtomicLong(key); |
| | | atomic.set(value); |
| | | } |
| | | |
| | | /** |
| | | * 获取原子值 |
| | | * |
| | | * @param key Redis键 |
| | | * @return 当前值 |
| | | */ |
| | | public static long getAtomicValue(String key) { |
| | | RAtomicLong atomic = CLIENT.getAtomicLong(key); |
| | | return atomic.get(); |
| | | } |
| | | |
| | | /** |
| | | * 递增原子值 |
| | | * |
| | | * @param key Redis键 |
| | | * @return 当前值 |
| | | */ |
| | | public static long incrAtomicValue(String key) { |
| | | RAtomicLong atomic = CLIENT.getAtomicLong(key); |
| | | return atomic.incrementAndGet(); |
| | | } |
| | | |
| | | /** |
| | | * 递减原子值 |
| | | * |
| | | * @param key Redis键 |
| | | * @return 当前值 |
| | | */ |
| | | public static long decrAtomicValue(String key) { |
| | | RAtomicLong atomic = CLIENT.getAtomicLong(key); |
| | | return atomic.decrementAndGet(); |
| | | } |
| | | |
| | | /** |
| | | * 获得缓存的基本对象列表 |
| | | * |
| | | * @param pattern 字符串前缀 |
| | | * @return 对象列表 |
| | | */ |
| | | public static Collection<String> keys(final String pattern) { |
| | | Iterable<String> iterable = CLIENT.getKeys().getKeysByPattern(pattern); |
| | | return IterUtil.toList(iterable); |
| | | Stream<String> stream = CLIENT.getKeys().getKeysStreamByPattern(getNameMapper().map(pattern)); |
| | | return stream.map(key -> getNameMapper().unmap(key)).collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 删除缓存的基本对象列表 |
| | | * |
| | | * @param pattern 字符串前缀 |
| | | */ |
| | | public static void deleteKeys(final String pattern) { |
| | | CLIENT.getKeys().deleteByPattern(getNameMapper().map(pattern)); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public static Boolean hasKey(String key) { |
| | | RKeys rKeys = CLIENT.getKeys(); |
| | | return rKeys.countExists(key) > 0; |
| | | return rKeys.countExists(getNameMapper().map(key)) > 0; |
| | | } |
| | | } |