From 662a0b9050e780a102c08314d85ecff00ea296b7 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Mon, 15 Apr 2024 11:14:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20redis=E6=8E=A5=E5=8F=A3=E6=89=A7=E8=A1=8C=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E8=B5=84=E6=BA=90=EF=BC=8C=E8=AE=BE=E7=BD=AE=E8=BF=87=E6=9C=9F?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/metersphere/api/service/ApiExecuteService.java | 3 ++- .../api/service/queue/ApiExecutionQueueService.java | 3 ++- .../api/service/queue/ApiExecutionSetService.java | 7 ++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java index 88faeb48eb..3bd3ae997f 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java @@ -56,6 +56,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import static io.metersphere.api.controller.result.ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR; @@ -151,7 +152,7 @@ public class ApiExecuteService { // 将测试脚本缓存到 redis String scriptRedisKey = getScriptRedisKey(taskRequest.getReportId(), taskRequest.getResourceId()); - stringRedisTemplate.opsForValue().set(scriptRedisKey, executeScript); + stringRedisTemplate.opsForValue().set(scriptRedisKey, executeScript, 1, TimeUnit.DAYS); if (StringUtils.equals(taskRequest.getRunMode(), ApiExecuteRunMode.FRONTEND_DEBUG.name())) { // 清空mino和kafka配置信息,避免前端获取 diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionQueueService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionQueueService.java index 2c22e47bfc..9099cdb61e 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionQueueService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionQueueService.java @@ -13,6 +13,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.LinkedList; import java.util.List; +import java.util.concurrent.TimeUnit; @Service public class ApiExecutionQueueService { @@ -26,7 +27,7 @@ public class ApiExecutionQueueService { @Transactional(propagation = Propagation.REQUIRES_NEW) public void insertQueue(ExecutionQueue queue, List queues) { // 保存队列信息 - redisTemplate.opsForValue().setIfAbsent(QUEUE_PREFIX + queue.getQueueId(), JSON.toJSONString(queue)); + redisTemplate.opsForValue().setIfAbsent(QUEUE_PREFIX + queue.getQueueId(), JSON.toJSONString(queue), 1, TimeUnit.DAYS); // 保存队列详情信息 queues.forEach(n -> redisTemplate.opsForList().rightPush(QUEUE_DETAIL_PREFIX + queue.getQueueId(), JSON.toJSONString(n))); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionSetService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionSetService.java index 590be14e3e..05ed852867 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionSetService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionSetService.java @@ -5,6 +5,7 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.util.List; +import java.util.concurrent.TimeUnit; @Service public class ApiExecutionSetService { @@ -21,7 +22,11 @@ public class ApiExecutionSetService { * @param resourceIds */ public void initSet(String setId, List resourceIds) { - resourceIds.forEach(resourceId -> redisTemplate.opsForSet().add(SET_PREFIX + setId, resourceId)); + resourceIds.forEach(resourceId -> { + String key = SET_PREFIX + setId; + redisTemplate.opsForSet().add(key, resourceId); + redisTemplate.expire(key, 1, TimeUnit.DAYS); + }); } /**