update 更新 RedisUtils set保留ttl 兼容 5.X redis版本
This commit is contained in:
parent
d2b7f8ef0f
commit
1010ccca4d
@ -97,12 +97,18 @@ public class RedisUtils {
|
|||||||
* @param key 缓存的键值
|
* @param key 缓存的键值
|
||||||
* @param value 缓存的值
|
* @param value 缓存的值
|
||||||
* @param isSaveTtl 是否保留TTL有效期(例如: set之前ttl剩余90 set之后还是为90)
|
* @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) {
|
public static <T> void setCacheObject(final String key, final T value, final boolean isSaveTtl) {
|
||||||
RBucket<Object> bucket = client.getBucket(key);
|
RBucket<Object> bucket = client.getBucket(key);
|
||||||
if (isSaveTtl) {
|
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 {
|
} else {
|
||||||
bucket.set(value);
|
bucket.set(value);
|
||||||
}
|
}
|
||||||
@ -116,7 +122,7 @@ public class RedisUtils {
|
|||||||
* @param timeout 时间
|
* @param timeout 时间
|
||||||
* @param timeUnit 时间颗粒度
|
* @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);
|
RBucket<T> result = client.getBucket(key);
|
||||||
result.set(value);
|
result.set(value);
|
||||||
result.expire(timeout, timeUnit);
|
result.expire(timeout, timeUnit);
|
||||||
@ -157,6 +163,17 @@ public class RedisUtils {
|
|||||||
return rBucket.get();
|
return rBucket.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得key剩余存活时间
|
||||||
|
*
|
||||||
|
* @param key 缓存键值
|
||||||
|
* @return 剩余存活时间
|
||||||
|
*/
|
||||||
|
public static <T> long getTimeToLive(final String key) {
|
||||||
|
RBucket<T> rBucket = client.getBucket(key);
|
||||||
|
return rBucket.remainTimeToLive();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除单个对象
|
* 删除单个对象
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user