feature: 增加对redis未部署时异常捕获 (#131)

* feature: 增加download-deer-flow

* feature: 增加对redis未部署时异常捕获

* feature: clean code
This commit is contained in:
hhhhsc701
2025-12-04 16:09:29 +08:00
committed by GitHub
parent 977d16339b
commit 7a9530c1e3
10 changed files with 107 additions and 38 deletions

View File

@@ -4,10 +4,10 @@ import com.datamate.common.infrastructure.exception.BusinessAssert;
import com.datamate.common.infrastructure.exception.SystemErrorCode;
import com.datamate.common.setting.domain.entity.SysParam;
import com.datamate.common.setting.domain.repository.SysParamRepository;
import com.datamate.common.setting.infrastructure.client.RedisClient;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Comparator;
@@ -24,7 +24,7 @@ import java.util.List;
@RequiredArgsConstructor
public class SysParamApplicationService {
private final SysParamRepository sysParamRepository;
private final StringRedisTemplate redisTemplate;
private final RedisClient redisClient;
/**
* 列表查询系统参数
@@ -48,14 +48,25 @@ public class SysParamApplicationService {
BusinessAssert.notNull(sysParam, SystemErrorCode.RESOURCE_NOT_FOUND);
sysParam.setParamValue(paramValue);
sysParamRepository.updateById(sysParam);
redisTemplate.opsForValue().set(sysParam.getParamKey(), paramValue);
redisClient.setParam(sysParam.getId(), paramValue);
}
public void deleteParamById(String paramId) {
SysParam sysParam = sysParamRepository.getById(paramId);
public void deleteParamById(String paramKey) {
SysParam sysParam = sysParamRepository.getById(paramKey);
BusinessAssert.notNull(sysParam, SystemErrorCode.RESOURCE_NOT_FOUND);
sysParamRepository.removeById(paramId);
redisTemplate.delete(sysParam.getParamKey());
sysParamRepository.removeById(paramKey);
redisClient.delParam(sysParam.getId());
}
public String getParamByKey(String paramId) {
String value = redisClient.getParam(paramId);
if (value == null) {
SysParam sysParam = sysParamRepository.getById(paramId);
if (sysParam != null) {
value = sysParam.getParamValue();
}
}
return value;
}
/**
@@ -65,7 +76,7 @@ public class SysParamApplicationService {
public void init() {
try {
List<SysParam> sysParams = sysParamRepository.list();
sysParams.forEach(sysParam -> redisTemplate.opsForValue().set(sysParam.getParamKey(), sysParam.getParamValue()));
sysParams.forEach(sysParam -> redisClient.setParam(sysParam.getId(), sysParam.getParamValue()));
} catch (Exception e) {
log.error("Init sys params to redis error", e);
}

View File

@@ -15,11 +15,6 @@ import lombok.Setter;
@Getter
@TableName("t_sys_param")
public class SysParam extends BaseEntity<String> {
/**
* 设置项键(唯一)
*/
private String paramKey;
/**
* 设置项值
*/

View File

@@ -0,0 +1,26 @@
package com.datamate.common.setting.infrastructure.client;
import com.datamate.common.setting.infrastructure.utils.FunctionUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@AllArgsConstructor
public class RedisClient {
private final StringRedisTemplate redisTemplate;
public void setParam(String key, String value) {
FunctionUtil.doWithoutThrow((k, v) -> redisTemplate.opsForValue().set(k, v), key, value);
}
public String getParam(String key) {
return FunctionUtil.getWithoutThrow((k) -> redisTemplate.opsForValue().get(k), key);
}
public void delParam(String key) {
FunctionUtil.doWithoutThrow(redisTemplate::delete, key);
}
}

View File

@@ -0,0 +1,35 @@
package com.datamate.common.setting.infrastructure.utils;
import lombok.extern.slf4j.Slf4j;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
@Slf4j
public class FunctionUtil {
public static <T, R> R getWithoutThrow(Function<T, R> action, T key) {
try {
return action.apply(key);
} catch (Exception e) {
log.warn(e.getMessage());
return null;
}
}
public static <T> void doWithoutThrow(Consumer<T> action, T key) {
try {
action.accept(key);
} catch (Exception e) {
log.warn(e.getMessage());
}
}
public static <T, R> void doWithoutThrow(BiConsumer<T, R> action, T t, R r) {
try {
action.accept(t, r);
} catch (Exception e) {
log.warn(e.getMessage());
}
}
}