fix 修复 RedisUtils 并发 set ttl 错误问题

2.X
疯狂的狮子li 3 years ago
parent 95d86cc2f6
commit 70ca157c07

@ -110,14 +110,13 @@ public class RedisUtils {
* @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);
RBucket<T> bucket = CLIENT.getBucket(key);
if (isSaveTtl) {
try {
bucket.setAndKeepTTL(value);
} catch (Exception e) {
long timeToLive = bucket.remainTimeToLive();
bucket.set(value);
bucket.expire(Duration.ofMillis(timeToLive));
setCacheObject(key, value, Duration.ofMillis(timeToLive));
}
} else {
bucket.set(value);
@ -132,9 +131,11 @@ public class RedisUtils {
* @param duration
*/
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(duration);
RBatch batch = CLIENT.createBatch();
RBucketAsync<T> bucket = batch.getBucket(key);
bucket.setAsync(value);
bucket.expireAsync(duration);
batch.execute();
}
/**

Loading…
Cancel
Save