|
|
@@ -1,29 +1,44 @@
|
|
|
package com.sckw.redis.utils;
|
|
|
|
|
|
-import org.redisson.api.RBucket;
|
|
|
-import org.redisson.api.RedissonClient;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.redisson.api.*;
|
|
|
+import org.redisson.client.codec.StringCodec;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.time.Duration;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
+@Slf4j
|
|
|
@Component
|
|
|
public class RedissonUtils {
|
|
|
|
|
|
- @Autowired
|
|
|
- RedissonClient redissonClient;
|
|
|
+ private RedissonClient redissonClient;
|
|
|
+
|
|
|
+ private static RedissonUtils redissonUtils;
|
|
|
+
|
|
|
+ public RedissonUtils(RedissonClient redissonClient) {
|
|
|
+ redissonUtils = this;
|
|
|
+ this.redissonClient = redissonClient;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 默认缓存时间
|
|
|
+ */
|
|
|
+ private static final Long DEFAULT_EXPIRED = 5 * 60L;
|
|
|
|
|
|
/**
|
|
|
* 添加缓存
|
|
|
- * @param key key
|
|
|
+ *
|
|
|
+ * @param key key
|
|
|
* @param value value
|
|
|
*/
|
|
|
- public void add(String key, String value) {
|
|
|
+ public static void add(String key, String value) {
|
|
|
//根据key获取bucket桶对象
|
|
|
- RBucket<Object> bucket = redissonClient.getBucket(key);
|
|
|
+ RBucket<Object> bucket = redissonUtils.redissonClient.getBucket(key);
|
|
|
//判读是否存在,并打印日志信息
|
|
|
if (!bucket.isExists()) {
|
|
|
//log.info("");
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
//log.info("update data");
|
|
|
}
|
|
|
//添加缓存,若已存在,则替换,设置缓存超时时间
|
|
|
@@ -32,33 +47,182 @@ public class RedissonUtils {
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * 在缓存中获取信息
|
|
|
+ * 删除缓存信息
|
|
|
+ *
|
|
|
* @param key key
|
|
|
- * @return 信息结果
|
|
|
*/
|
|
|
- public Object get(String key) {
|
|
|
+ public static void delete(String key) {
|
|
|
//根据key获取bucket桶对象
|
|
|
- RBucket<Object> bucket = redissonClient.getBucket(key);
|
|
|
+ RBucket<Object> bucket = redissonUtils.redissonClient.getBucket(key);
|
|
|
//判读是否存在,并打印日志信息
|
|
|
if (!bucket.isExists()) {
|
|
|
//log.info("error");
|
|
|
}
|
|
|
- //log.info("cache is {}", bucket.get());
|
|
|
+ bucket.delete();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断缓存是否存在
|
|
|
+ *
|
|
|
+ * @param key
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean exists(String key) {
|
|
|
+ return redissonUtils.redissonClient.getBucket(key).isExists();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取缓存
|
|
|
+ *
|
|
|
+ * @param key 缓存key
|
|
|
+ * @param <T>
|
|
|
+ * @return 缓存返回值
|
|
|
+ */
|
|
|
+ public static <T> T get(String key) {
|
|
|
+ RBucket<T> bucket = redissonUtils.redissonClient.getBucket(key);
|
|
|
return bucket.get();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 删除缓存信息
|
|
|
- * @param key key
|
|
|
+ * 以string的方式读取缓存
|
|
|
+ *
|
|
|
+ * @param key 缓存key
|
|
|
+ * @return 缓存返回值
|
|
|
*/
|
|
|
- public void delete(String key) {
|
|
|
- //根据key获取bucket桶对象
|
|
|
- RBucket<Object> bucket = redissonClient.getBucket(key);
|
|
|
- //判读是否存在,并打印日志信息
|
|
|
- if (!bucket.isExists()) {
|
|
|
- //log.info("error");
|
|
|
- }
|
|
|
- bucket.delete();
|
|
|
+ public static String getString(String key) {
|
|
|
+ RBucket<String> bucket = redissonUtils.redissonClient.getBucket(key, StringCodec.INSTANCE);
|
|
|
+ return bucket.get();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置缓存(注:redisson会自动选择序列化反序列化方式)
|
|
|
+ *
|
|
|
+ * @param key 缓存key
|
|
|
+ * @param value 缓存值
|
|
|
+ * @param <T>
|
|
|
+ */
|
|
|
+ public static <T> void put(String key, T value) {
|
|
|
+ log.debug("添加缓存【{}】 【{}】开始", key, value);
|
|
|
+ RBucket<T> bucket = redissonUtils.redissonClient.getBucket(key);
|
|
|
+ bucket.set(value, DEFAULT_EXPIRED, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 以string的方式设置缓存
|
|
|
+ *
|
|
|
+ * @param key
|
|
|
+ * @param value
|
|
|
+ */
|
|
|
+ public static void putString(String key, String value) {
|
|
|
+ log.debug("添加缓存【{}】 【{}】开始", key, value);
|
|
|
+ RBucket<String> bucket = redissonUtils.redissonClient.getBucket(key, StringCodec.INSTANCE);
|
|
|
+ bucket.set(value, DEFAULT_EXPIRED, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 如果不存在则写入缓存(string方式,不带有redisson的格式信息)
|
|
|
+ *
|
|
|
+ * @param key 缓存key
|
|
|
+ * @param value 缓存值
|
|
|
+ * @param expired 缓存过期时间
|
|
|
+ */
|
|
|
+ public boolean putStringIfAbsent(String key, String value, long expired) {
|
|
|
+ log.debug("如果不存在则写入缓存【{}】 【{}】 【{}】开始", key, value, expired);
|
|
|
+ RBucket<String> bucket = redissonUtils.redissonClient.getBucket(key, StringCodec.INSTANCE);
|
|
|
+ return bucket.setIfAbsent(value, expired <= 0 ? Duration.ofSeconds(DEFAULT_EXPIRED) : Duration.ofSeconds(expired));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 如果不存在则写入缓存(string方式,不带有redisson的格式信息)(不带过期时间,永久保存)
|
|
|
+ *
|
|
|
+ * @param key 缓存key
|
|
|
+ * @param value 缓存值
|
|
|
+ */
|
|
|
+ public boolean putStringIfAbsent(String key, String value) {
|
|
|
+ log.debug("如果不存在则写入永久缓存【{}】 【{}】开始", key, value);
|
|
|
+ RBucket<String> bucket = redissonUtils.redissonClient.getBucket(key, StringCodec.INSTANCE);
|
|
|
+ return bucket.setIfAbsent(value);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置缓存
|
|
|
+ *
|
|
|
+ * @param key 缓存key
|
|
|
+ * @param value 缓存值
|
|
|
+ * @param expired 缓存过期时间
|
|
|
+ * @param <T> 类型
|
|
|
+ */
|
|
|
+ public static <T> void put(String key, T value, long expired) {
|
|
|
+ log.debug("如果不存在则写入缓存【{}】 【{}】 【{}】开始", key, value, expired);
|
|
|
+ RBucket<T> bucket = redissonUtils.redissonClient.getBucket(key);
|
|
|
+ bucket.set(value, expired <= 0 ? DEFAULT_EXPIRED : expired, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 移除缓存
|
|
|
+ *
|
|
|
+ * @param key
|
|
|
+ */
|
|
|
+ public static void remove(String key) {
|
|
|
+ log.debug("移除缓存【{}】开始", key);
|
|
|
+ redissonUtils.redissonClient.getBucket(key).delete();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 暴露redisson的RList对象
|
|
|
+ *
|
|
|
+ * @param key
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T> RList<T> getRedisList(String key) {
|
|
|
+ return redissonUtils.redissonClient.getList(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 暴露redisson的RMapCache对象
|
|
|
+ *
|
|
|
+ * @param key
|
|
|
+ * @param <K>
|
|
|
+ * @param <V>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <K, V> RMapCache<K, V> getRedisMap(String key) {
|
|
|
+ return redissonUtils.redissonClient.getMapCache(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 暴露redisson的RSET对象
|
|
|
+ *
|
|
|
+ * @param key
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T> RSet<T> getRedisSet(String key) {
|
|
|
+ return redissonUtils.redissonClient.getSet(key);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 暴露redisson的RScoredSortedSet对象
|
|
|
+ *
|
|
|
+ * @param key
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T> RScoredSortedSet<T> getRedisScoredSortedSet(String key) {
|
|
|
+ return redissonUtils.redissonClient.getScoredSortedSet(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 暴露redisson的Lock对象
|
|
|
+ *
|
|
|
+ * @param key
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static RLock getRedisLock(String key) {
|
|
|
+ return redissonUtils.redissonClient.getLock(key);
|
|
|
}
|
|
|
|
|
|
}
|