REQ-2516-修复设置请求记录并发冲突导致插入多条数据问题

This commit is contained in:
yangqicheng 2024-07-10 10:45:53 +08:00
parent 74665da129
commit 8bd022dfab
2 changed files with 15 additions and 3 deletions

View File

@ -91,9 +91,9 @@ public class RequestHeaderContextInterceptor implements HandlerInterceptor {
String cacheRepeatKey = REPEAT_KEY + applicationName;
log.info("repeatApi key: {}", cacheRepeatKey);
String key = RedisUtils.getCacheObject(cacheRepeatKey);
if (Objects.isNull(key)) {
RedisUtils.setCacheObject(cacheRepeatKey, "", Duration.ofSeconds(5));
//success为true表示key不存在,执行成功,false表示key存在,执行失败
Boolean success = RedisUtils.trySetObject(cacheRepeatKey, "", Duration.ofSeconds(5));
if (success) {
KEY_CACHE.set(cacheRepeatKey);
insert(extAxProperty, applicationName, clientVersion, manageableStatus);
}

View File

@ -7,6 +7,7 @@ import org.redisson.api.RAtomicLong;
import org.redisson.api.RBatch;
import org.redisson.api.RBucket;
import org.redisson.api.RBucketAsync;
import org.redisson.api.RFuture;
import org.redisson.api.RKeys;
import org.redisson.api.RList;
import org.redisson.api.RMap;
@ -23,6 +24,7 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -140,6 +142,16 @@ public class RedisUtils {
batch.execute();
}
public static <T> Boolean trySetObject(final String key, final T value, final Duration duration) {
RBatch batch = CLIENT.createBatch();
RBucketAsync<T> bucket = batch.getBucket(key);
RFuture<Boolean> future = bucket.trySetAsync(value, duration.toMillis(), TimeUnit.MILLISECONDS);
batch.execute();
return future.join();
}
/**
* 注册对象监听器
* <p>