fix 修复 RedisUtils 并发 set ttl 错误问题
This commit is contained in:
parent
975b84a394
commit
a8d798c38a
@ -110,14 +110,13 @@ public class RedisUtils {
|
|||||||
* @since Redis 6.X 以上使用 setAndKeepTTL 兼容 5.X 方案
|
* @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<T> bucket = CLIENT.getBucket(key);
|
||||||
if (isSaveTtl) {
|
if (isSaveTtl) {
|
||||||
try {
|
try {
|
||||||
bucket.setAndKeepTTL(value);
|
bucket.setAndKeepTTL(value);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
long timeToLive = bucket.remainTimeToLive();
|
long timeToLive = bucket.remainTimeToLive();
|
||||||
bucket.set(value);
|
setCacheObject(key, value, Duration.ofMillis(timeToLive));
|
||||||
bucket.expire(Duration.ofMillis(timeToLive));
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
bucket.set(value);
|
bucket.set(value);
|
||||||
@ -132,9 +131,11 @@ public class RedisUtils {
|
|||||||
* @param duration 时间
|
* @param duration 时间
|
||||||
*/
|
*/
|
||||||
public static <T> void setCacheObject(final String key, final T value, final Duration duration) {
|
public static <T> void setCacheObject(final String key, final T value, final Duration duration) {
|
||||||
RBucket<T> result = CLIENT.getBucket(key);
|
RBatch batch = CLIENT.createBatch();
|
||||||
result.set(value);
|
RBucketAsync<T> bucket = batch.getBucket(key);
|
||||||
result.expire(duration);
|
bucket.setAsync(value);
|
||||||
|
bucket.expireAsync(duration);
|
||||||
|
batch.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user