update 优化 重写字典查询 使用本地缓存优化网络开销 提升到上级实现减少rpc调用频率 使用流处理减少字符串操作
parent
ac88e8784b
commit
ec60b9a1e8
|
|
@ -1,5 +1,9 @@
|
||||||
package com.ruoyi.system.api;
|
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 {
|
public interface RemoteDictService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据字典类型和字典值获取字典标签
|
* 根据字典类型查询字典数据
|
||||||
*
|
*
|
||||||
* @param dictType 字典类型
|
* @param dictType 字典类型
|
||||||
* @param dictValue 字典值
|
* @return 字典数据集合信息
|
||||||
* @param separator 分隔符
|
|
||||||
* @return 字典标签
|
|
||||||
*/
|
*/
|
||||||
String getDictLabel(String dictType, String dictValue, String separator);
|
List<SysDictData> selectDictDataByType(String dictType);
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据字典类型和字典标签获取字典值
|
|
||||||
*
|
|
||||||
* @param dictType 字典类型
|
|
||||||
* @param dictLabel 字典标签
|
|
||||||
* @param separator 分隔符
|
|
||||||
* @return 字典值
|
|
||||||
*/
|
|
||||||
String getDictValue(String dictType, String dictLabel, String separator);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,10 @@
|
||||||
<artifactId>dubbo-spring-boot-starter</artifactId>
|
<artifactId>dubbo-spring-boot-starter</artifactId>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.dev33</groupId>
|
||||||
|
<artifactId>sa-token-core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,21 @@
|
||||||
package com.ruoyi.common.dict.service.impl;
|
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.service.DictService;
|
||||||
|
import com.ruoyi.common.core.utils.StringUtils;
|
||||||
import com.ruoyi.system.api.RemoteDictService;
|
import com.ruoyi.system.api.RemoteDictService;
|
||||||
|
import com.ruoyi.system.api.domain.SysDictData;
|
||||||
import org.apache.dubbo.config.annotation.DubboReference;
|
import org.apache.dubbo.config.annotation.DubboReference;
|
||||||
import org.springframework.stereotype.Service;
|
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 分隔符
|
* @param separator 分隔符
|
||||||
* @return 字典标签
|
* @return 字典标签
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unchecked cast")
|
||||||
@Override
|
@Override
|
||||||
public String getDictLabel(String dictType, String dictValue, String separator) {
|
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 分隔符
|
* @param separator 分隔符
|
||||||
* @return 字典值
|
* @return 字典值
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unchecked cast")
|
||||||
@Override
|
@Override
|
||||||
public String getDictValue(String dictType, String dictLabel, String separator) {
|
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;
|
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.RemoteDictService;
|
||||||
import com.ruoyi.system.api.domain.SysDictData;
|
import com.ruoyi.system.api.domain.SysDictData;
|
||||||
import com.ruoyi.system.service.ISysDictTypeService;
|
import com.ruoyi.system.service.ISysDictTypeService;
|
||||||
|
|
@ -26,50 +23,8 @@ public class RemoteDictServiceImpl implements RemoteDictService {
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getDictLabel(String dictType, String dictValue, String separator) {
|
public List<SysDictData> selectDictDataByType(String dictType) {
|
||||||
StringBuilder propertyString = new StringBuilder();
|
return sysDictTypeService.selectDictDataByType(dictType);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@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…
Reference in New Issue