selectDeptList 使用全异步处理与加二级缓存

This commit is contained in:
wangliwen 2022-06-28 11:49:54 +08:00
parent 2954ede07a
commit d16d7add39
2 changed files with 46 additions and 33 deletions

View File

@ -38,7 +38,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -546,9 +546,9 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
} }
@Override @Override
@CachePut(cacheNames = {selectDeptListKey}, key = "#p1") @Cacheable(value = selectDeptListKey, key = "#p0")
public Object selectDeptList(JSONObject jsonObject, String type) { public Object selectDeptList(JSONObject jsonObject, String type) {
List<Map> resultList = new CopyOnWriteArrayList<Map>(); List<Map> resultList = new CopyOnWriteArrayList<>();
HashMap<String, Object> resourceMap = new HashMap<>(); HashMap<String, Object> resourceMap = new HashMap<>();
resourceMap.put("type", "全部能力目录"); resourceMap.put("type", "全部能力目录");
QueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper<>(); QueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper<>();
@ -561,7 +561,9 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
} }
Map<String, List<Map<String, Object>>> listMap = typeMapList.stream().collect(Collectors.groupingBy(m -> m.get("type").toString())); Map<String, List<Map<String, Object>>> listMap = typeMapList.stream().collect(Collectors.groupingBy(m -> m.get("type").toString()));
//区级要根据行政区划多加一层结构 //区级要根据行政区划多加一层结构
listMap.entrySet().parallelStream().filter(index -> !"区级".equals(index.getKey())).forEach(item -> { List<CompletableFuture> tasks =
listMap.entrySet().stream().filter(index -> !"区级".equals(index.getKey())).map(item -> {
CompletableFuture task = CompletableFuture.runAsync(() -> {
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
map.put("type", item.getKey()); map.put("type", item.getKey());
Integer integer = resourceDao.selectTypeCountByDept(item.getKey(), jsonObject.getString("type")); Integer integer = resourceDao.selectTypeCountByDept(item.getKey(), jsonObject.getString("type"));
@ -572,6 +574,10 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
resultList.add(map); resultList.add(map);
} }
}); });
return task;
}).collect(Collectors.toList());
CompletableFuture.allOf(tasks.toArray(new CompletableFuture[tasks.size()])).join();
Optional<List<Map<String, Object>>> areaList = Optional.ofNullable(listMap.get("区级")); Optional<List<Map<String, Object>>> areaList = Optional.ofNullable(listMap.get("区级"));
Optional<Map<String, List<Map<String, Object>>>> areaTypeList = Optional.ofNullable(areaList.orElse(new ArrayList<>()).stream().collect(Collectors.groupingBy(m -> m.get("districtName").toString()))); Optional<Map<String, List<Map<String, Object>>>> areaTypeList = Optional.ofNullable(areaList.orElse(new ArrayList<>()).stream().collect(Collectors.groupingBy(m -> m.get("districtName").toString())));
HashMap<Object, Object> areaMap = new HashMap<>(); HashMap<Object, Object> areaMap = new HashMap<>();
@ -581,8 +587,10 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
integer = 0; integer = 0;
} }
areaMap.put("total", integer); areaMap.put("total", integer);
ArrayList<Map> areaListTemp = new ArrayList<>(); List<Map> areaListTemp = new CopyOnWriteArrayList<>();
List<CompletableFuture> tasksArea = new ArrayList<>();
areaTypeList.orElse(new HashMap<>()).forEach((key, value) -> { areaTypeList.orElse(new HashMap<>()).forEach((key, value) -> {
CompletableFuture task = CompletableFuture.runAsync(() -> {
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
map.put("type", key); map.put("type", key);
map.put("total", resourceDao.selectTypeCountByDist(key, jsonObject.getString("type"))); map.put("total", resourceDao.selectTypeCountByDist(key, jsonObject.getString("type")));
@ -590,6 +598,9 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
map.put("dataList", value); map.put("dataList", value);
areaListTemp.add(map); areaListTemp.add(map);
}); });
tasksArea.add(task);
});
CompletableFuture.allOf(tasksArea.toArray(new CompletableFuture[tasksArea.size()])).join();
areaMap.put("dataList", areaListTemp); areaMap.put("dataList", areaListTemp);
if (integer != 0) { if (integer != 0) {
resultList.add(areaMap); resultList.add(areaMap);

View File

@ -10,6 +10,7 @@ alter table `tb_data_attr` drop index `attr_value`;
alter table `tb_data_resource` drop index `name`; alter table `tb_data_resource` drop index `name`;
alter table `tb_data_resource` drop index `deptId`; alter table `tb_data_resource` drop index `deptId`;
alter table `sys_dept` drop index `type`; alter table `sys_dept` drop index `type`;
alter table `sys_dept` drop index `district`;
-- 创建索引 -- 创建索引
alter table `tb_resource_collection` ADD INDEX `resourceid`(`resource_id`) USING BTREE comment '收藏的资源id'; alter table `tb_resource_collection` ADD INDEX `resourceid`(`resource_id`) USING BTREE comment '收藏的资源id';
alter table`tb_resource_car` ADD INDEX `resourceid`(`resource_id`) USING BTREE comment '加入申购车的id'; alter table`tb_resource_car` ADD INDEX `resourceid`(`resource_id`) USING BTREE comment '加入申购车的id';
@ -22,3 +23,4 @@ alter table `tb_data_attr` ADD FULLTEXT INDEX `attr_value`(`attr_value`);
alter table `tb_data_resource` ADD FULLTEXT INDEX `name`(`name`); alter table `tb_data_resource` ADD FULLTEXT INDEX `name`(`name`);
ALTER TABLE `tb_data_resource` ADD INDEX `deptId`(`dept_id`) USING BTREE; ALTER TABLE `tb_data_resource` ADD INDEX `deptId`(`dept_id`) USING BTREE;
ALTER TABLE `sys_dept` ADD INDEX `type`(`type`) USING BTREE; ALTER TABLE `sys_dept` ADD INDEX `type`(`type`) USING BTREE;
ALTER TABLE `sys_dept` ADD INDEX `district`(`district`) USING BTREE;