|
@@ -97,12 +97,18 @@ public class RedisUtils {
|
|
|
* @param key 缓存的键值
|
|
|
* @param value 缓存的值
|
|
|
* @param isSaveTtl 是否保留TTL有效期(例如: set之前ttl剩余90 set之后还是为90)
|
|
|
- * @since Redis 6.0.0 以上有效
|
|
|
+ * @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) {
|
|
|
- bucket.setAndKeepTTL(value);
|
|
|
+ try {
|
|
|
+ bucket.setAndKeepTTL(value);
|
|
|
+ } catch (Exception e) {
|
|
|
+ long timeToLive = bucket.remainTimeToLive();
|
|
|
+ bucket.set(value);
|
|
|
+ bucket.expire(timeToLive, TimeUnit.MILLISECONDS);
|
|
|
+ }
|
|
|
} else {
|
|
|
bucket.set(value);
|
|
|
}
|
|
@@ -116,7 +122,7 @@ public class RedisUtils {
|
|
|
* @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);
|
|
@@ -157,6 +163,17 @@ public class RedisUtils {
|
|
|
return rBucket.get();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获得key剩余存活时间
|
|
|
+ *
|
|
|
+ * @param key 缓存键值
|
|
|
+ * @return 剩余存活时间
|
|
|
+ */
|
|
|
+ public static <T> long getTimeToLive(final String key) {
|
|
|
+ RBucket<T> rBucket = client.getBucket(key);
|
|
|
+ return rBucket.remainTimeToLive();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 删除单个对象
|
|
|
*
|