update 优化 重写字典查询 使用本地缓存优化网络开销 提升到上级实现减少rpc调用频率 使用流处理减少字符串操作

2.X
疯狂的狮子li 2 years ago
parent ac88e8784b
commit ec60b9a1e8

@ -1,5 +1,9 @@
package com.ruoyi.system.api;
import com.ruoyi.system.api.domain.SysDictData;
import java.util.List;
/**
*
*
@ -8,22 +12,10 @@ package com.ruoyi.system.api;
public interface RemoteDictService {
/**
*
*
* @param dictType
* @param dictValue
* @param separator
* @return
*/
String getDictLabel(String dictType, String dictValue, String separator);
/**
*
*
*
* @param dictType
* @param dictLabel
* @param separator
* @return
* @param dictType
* @return
*/
String getDictValue(String dictType, String dictLabel, String separator);
List<SysDictData> selectDictDataByType(String dictType);
}

@ -33,6 +33,10 @@
<artifactId>dubbo-spring-boot-starter</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-core</artifactId>
</dependency>
</dependencies>
</project>

@ -1,10 +1,21 @@
package com.ruoyi.common.dict.service.impl;
import cn.dev33.satoken.context.SaHolder;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.service.DictService;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.RemoteDictService;
import com.ruoyi.system.api.domain.SysDictData;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
*
*
@ -24,9 +35,24 @@ public class DictServiceImpl implements DictService {
* @param separator
* @return
*/
@SuppressWarnings("unchecked cast")
@Override
public String getDictLabel(String dictType, String dictValue, String separator) {
return remoteDictService.getDictLabel(dictType, dictValue, separator);
// 优先从本地缓存获取
List<SysDictData> datas = (List<SysDictData>) SaHolder.getStorage().get(CacheConstants.SYS_DICT_KEY + dictType);
if (ObjectUtil.isNull(datas)) {
datas = remoteDictService.selectDictDataByType(dictType);
SaHolder.getStorage().set(CacheConstants.SYS_DICT_KEY + dictType, datas);
}
Map<String, String> map = CollStreamUtil.toMap(datas, SysDictData::getDictValue, SysDictData::getDictLabel);
if (StringUtils.containsAny(dictValue, separator)) {
return Arrays.stream(dictValue.split(separator))
.map(v -> map.getOrDefault(v, StringUtils.EMPTY))
.collect(Collectors.joining(separator));
} else {
return map.getOrDefault(dictValue, StringUtils.EMPTY);
}
}
/**
@ -37,9 +63,24 @@ public class DictServiceImpl implements DictService {
* @param separator
* @return
*/
@SuppressWarnings("unchecked cast")
@Override
public String getDictValue(String dictType, String dictLabel, String separator) {
return remoteDictService.getDictValue(dictType, dictLabel, separator);
// 优先从本地缓存获取
List<SysDictData> datas = (List<SysDictData>) SaHolder.getStorage().get(CacheConstants.SYS_DICT_KEY + dictType);
if (ObjectUtil.isNull(datas)) {
datas = remoteDictService.selectDictDataByType(dictType);
SaHolder.getStorage().set(CacheConstants.SYS_DICT_KEY + dictType, datas);
}
Map<String, String> map = CollStreamUtil.toMap(datas, SysDictData::getDictLabel, SysDictData::getDictValue);
if (StringUtils.containsAny(dictLabel, separator)) {
return Arrays.stream(dictLabel.split(separator))
.map(l -> map.getOrDefault(l, StringUtils.EMPTY))
.collect(Collectors.joining(separator));
} else {
return map.getOrDefault(dictLabel, StringUtils.EMPTY);
}
}
}

@ -1,8 +1,5 @@
package com.ruoyi.system.dubbo;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.RemoteDictService;
import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.system.service.ISysDictTypeService;
@ -26,50 +23,8 @@ public class RemoteDictServiceImpl implements RemoteDictService {
@Override
public String getDictLabel(String dictType, String dictValue, String separator) {
StringBuilder propertyString = new StringBuilder();
List<SysDictData> datas = sysDictTypeService.selectDictDataByType(dictType);
if (StringUtils.containsAny(dictValue, separator) && CollUtil.isNotEmpty(datas)) {
for (SysDictData dict : datas) {
for (String value : dictValue.split(separator)) {
if (value.equals(dict.getDictValue())) {
propertyString.append(dict.getDictLabel() + separator);
break;
}
}
}
} else {
for (SysDictData dict : datas) {
if (dictValue.equals(dict.getDictValue())) {
return dict.getDictLabel();
}
}
}
return StringUtils.stripEnd(propertyString.toString(), separator);
public List<SysDictData> selectDictDataByType(String dictType) {
return sysDictTypeService.selectDictDataByType(dictType);
}
@Override
public String getDictValue(String dictType, String dictLabel, String separator) {
StringBuilder propertyString = new StringBuilder();
List<SysDictData> datas = sysDictTypeService.selectDictDataByType(dictType);
if (StringUtils.containsAny(dictLabel, separator) && CollUtil.isNotEmpty(datas)) {
for (SysDictData dict : datas) {
for (String label : dictLabel.split(separator)) {
if (label.equals(dict.getDictLabel())) {
propertyString.append(dict.getDictValue() + separator);
break;
}
}
}
} else {
for (SysDictData dict : datas) {
if (dictLabel.equals(dict.getDictLabel())) {
return dict.getDictValue();
}
}
}
return StringUtils.stripEnd(propertyString.toString(), separator);
}
}

Loading…
Cancel
Save