翻译代码 public String getState(String key) { String value = this.cache.get(key); if (value == null) { value = fluentTemplate .withHeader(RedisConstants.COMMAND, "HGET") .withHeader(RedisConstants.KEY, keyPrefix) .withHeader(RedisConstants.FIELD, key) .to("redis://127.0.0.1:6379") .request(String.class); if (value != null) { this.cache.put(key, value); } } return value; }
时间: 2024-02-10 10:34:13 浏览: 134
public String getState(String key) {
// 从缓存中获取数据
String value = this.cache.get(key);
// 如果缓存中没有该数据
if (value == null) {
// 从 Redis 中获取数据
value = fluentTemplate
.withHeader(RedisConstants.COMMAND, "HGET")
.withHeader(RedisConstants.KEY, keyPrefix)
.withHeader(RedisConstants.FIELD, key)
.to("redis://127.0.0.1:6379")
.request(String.class);
// 如果 Redis 中存在该数据
if (value != null) {
// 将数据放入缓存中
this.cache.put(key, value);
}
}
// 返回数据
return value;
}
相关问题
优化以下代码: for (DqExecuteResult dqExecuteResult : dqExecuteResults) { // 一致性 if (RuleTemplateName.MULTI_TABLE_ACCURACY.getCode().equals(dqExecuteResult.getRuleName())) { coherenceTotal += dqExecuteResult.getTotal(); if (DqTaskState.FAILURE.getCode() == dqExecuteResult.getState()) { coherenceSuccess += (dqExecuteResult.getTotal() - Math.round(dqExecuteResult.getStatisticsValue())); } else{ coherenceSuccess += dqExecuteResult.getTotal(); } } // 完备性 else if(RuleTemplateName.NULL_CHECK.getCode().equals(dqExecuteResult.getRuleName())){ completeTotal += dqExecuteResult.getTotal(); if (DqTaskState.FAILURE.getCode() == dqExecuteResult.getState()) { completeSuccess += (dqExecuteResult.getTotal() - Math.round(dqExecuteResult.getStatisticsValue())); } else{ completeSuccess += dqExecuteResult.getTotal(); } } // 有效性 else { effectiveTotal += dqExecuteResult.getTotal(); if (DqTaskState.FAILURE.getCode() == dqExecuteResult.getState()) { effectiveSuccess += (dqExecuteResult.getTotal() - Math.round(dqExecuteResult.getStatisticsValue())); } else{ effectiveSuccess += dqExecuteResult.getTotal(); } } }
可以考虑以下优化:
1. 将重复的代码提取出来,避免重复书写,可以提高代码的可读性和维护性;
2. 可以使用 switch-case 语句替代 if-else if 语句,使代码更加简洁;
3. 可以将常量提取出来,避免在代码中多次出现,提高可维护性。
下面是优化后的代码:
```
final String MULTI_TABLE_ACCURACY = RuleTemplateName.MULTI_TABLE_ACCURACY.getCode();
final String NULL_CHECK = RuleTemplateName.NULL_CHECK.getCode();
for (DqExecuteResult dqExecuteResult : dqExecuteResults) {
String ruleName = dqExecuteResult.getRuleName();
int state = dqExecuteResult.getState();
int total = dqExecuteResult.getTotal();
double statisticsValue = dqExecuteResult.getStatisticsValue();
switch (ruleName) {
case MULTI_TABLE_ACCURACY:
coherenceTotal += total;
coherenceSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total;
break;
case NULL_CHECK:
completeTotal += total;
completeSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total;
break;
default:
effectiveTotal += total;
effectiveSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total;
}
}
```
优化以下代码: // 初始化结果 long coherenceTotal = 0; long effectiveTotal = 0; long completeTotal = 0; long coherenceSuccess = 0; long effectiveSuccess = 0; long completeSuccess = 0; long lastCoherenceTotal = 0; long lastEffectiveTotal = 0; long lastCompleteTotal = 0; long lastCoherenceSuccess = 0; long lastEffectiveSuccess = 0; long lastCompleteSuccess = 0; // 平均结果 for (DqExecuteResult dqExecuteResult : dqExecuteResults) { String ruleName = dqExecuteResult.getRuleName(); int state = dqExecuteResult.getState(); int total = dqExecuteResult.getTotal(); double statisticsValue = dqExecuteResult.getStatisticsValue(); switch (ruleName) { case "(multi_table_accuracy)": coherenceTotal += total; coherenceSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total; break; case "(null_check)": completeTotal += total; completeSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total; break; default: effectiveTotal += total; effectiveSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total; } } // 最新一次结果 for (DqExecuteResult dqExecuteResult : lastDqExecuteResults) { String ruleName = dqExecuteResult.getRuleName(); int state = dqExecuteResult.getState(); int total = dqExecuteResult.getTotal(); double statisticsValue = dqExecuteResult.getStatisticsValue(); switch (ruleName) { case "(multi_table_accuracy)": lastCoherenceTotal += total; lastCoherenceSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total; break; case "(null_check)": lastCompleteTotal += total; lastCompleteSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total; break; default: lastEffectiveTotal += total; lastEffectiveSuccess += (state == DqTaskState.FAILURE.getCode()) ? (total - Math.round(statisticsValue)) : total; } }
可以对代码进行以下优化:
1. 使用数组或者集合来存储结果,而不是一一定义变量,这样可以减少代码量,并且更易于扩展和维护。
2. 在计算成功数的时候,可以使用三目运算符来判断是否需要减去统计值,这样可以减少重复代码。
3. 可以将相同的代码块提取出来,减少重复代码。
优化后的代码如下:
```
// 初始化结果
long[] total = new long[3];
long[] success = new long[3];
long[] lastTotal = new long[3];
long[] lastSuccess = new long[3];
// 平均结果
for (DqExecuteResult dqExecuteResult : dqExecuteResults) {
String ruleName = dqExecuteResult.getRuleName();
int state = dqExecuteResult.getState();
int index = getIndex(ruleName);
total[index] += dqExecuteResult.getTotal();
success[index] += (state == DqTaskState.FAILURE.getCode()) ? (dqExecuteResult.getTotal() - Math.round(dqExecuteResult.getStatisticsValue())) : dqExecuteResult.getTotal();
}
// 最新一次结果
for (DqExecuteResult dqExecuteResult : lastDqExecuteResults) {
String ruleName = dqExecuteResult.getRuleName();
int state = dqExecuteResult.getState();
int index = getIndex(ruleName);
lastTotal[index] += dqExecuteResult.getTotal();
lastSuccess[index] += (state == DqTaskState.FAILURE.getCode()) ? (dqExecuteResult.getTotal() - Math.round(dqExecuteResult.getStatisticsValue())) : dqExecuteResult.getTotal();
}
// 根据规则名获取下标
private int getIndex(String ruleName) {
switch (ruleName) {
case "(multi_table_accuracy)":
return 0;
case "(null_check)":
return 1;
default:
return 2;
}
}
```
阅读全文