| | |
| | | import lombok.NoArgsConstructor; |
| | | import org.redisson.api.*; |
| | | |
| | | 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; |
| | | |
| | | /** |
| | |
| | | } catch (Exception e) { |
| | | long timeToLive = bucket.remainTimeToLive(); |
| | | bucket.set(value); |
| | | bucket.expire(timeToLive, TimeUnit.MILLISECONDS); |
| | | bucket.expire(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) { |
| | | public static <T> void setCacheObject(final String key, final T value, final Duration duration) { |
| | | RBucket<T> result = CLIENT.getBucket(key); |
| | | result.set(value); |
| | | result.expire(timeout, timeUnit); |
| | | result.expire(duration); |
| | | } |
| | | |
| | | /** |
| | | * 注册对象监听器 |
| | | * |
| | | * <p> |
| | | * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置 |
| | | * |
| | | * @param key 缓存的键值 |
| | |
| | | * @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); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | /** |
| | | * 注册List监听器 |
| | | * |
| | | * <p> |
| | | * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置 |
| | | * |
| | | * @param key 缓存的键值 |
| | |
| | | |
| | | /** |
| | | * 注册Set监听器 |
| | | * |
| | | * <p> |
| | | * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置 |
| | | * |
| | | * @param key 缓存的键值 |
| | |
| | | |
| | | /** |
| | | * 注册Map监听器 |
| | | * |
| | | * <p> |
| | | * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置 |
| | | * |
| | | * @param key 缓存的键值 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 设置原子值 |
| | | * |
| | | * @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 字符串前缀 |
| | |
| | | Iterable<String> iterable = CLIENT.getKeys().getKeysByPattern(pattern); |
| | | return IterUtil.toList(iterable); |
| | | } |
| | | |
| | | /** |
| | | * 检查redis中是否存在key |
| | | * |
| | | * @param key 键 |
| | | */ |
| | | public static Boolean hasKey(String key) { |
| | | RKeys rKeys = CLIENT.getKeys(); |
| | | return rKeys.countExists(key) > 0; |
| | | } |
| | | } |