Commit 8033818e by xmh

缓存

1 parent 1b130e8f
package com.viontech.storage.entity; package com.viontech.storage.entity;
import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Pair; import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.XmlUtil; import cn.hutool.core.util.XmlUtil;
...@@ -21,6 +23,7 @@ import org.w3c.dom.Document; ...@@ -21,6 +23,7 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import java.time.Duration;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -35,7 +38,7 @@ import java.util.stream.Collectors; ...@@ -35,7 +38,7 @@ import java.util.stream.Collectors;
public class Generator { public class Generator {
private static final String ROOT_ELEMENT_NAME = "StorageConfig"; private static final String ROOT_ELEMENT_NAME = "StorageConfig";
private static final String FILE_VERSION = "001-012-000-000"; private static final String FILE_VERSION = "001-012-000-000";
private static final TimedCache<Long, Generator> CACHE = CacheUtil.newTimedCache(Duration.ofSeconds(5).toMillis());
private final Document document = XmlUtil.createXml(ROOT_ELEMENT_NAME); private final Document document = XmlUtil.createXml(ROOT_ELEMENT_NAME);
/** 来自 StorageConfig, 对应 Solution */ /** 来自 StorageConfig, 对应 Solution */
private final List<Context> contexts; private final List<Context> contexts;
...@@ -43,56 +46,69 @@ public class Generator { ...@@ -43,56 +46,69 @@ public class Generator {
private final Map<Long, PicConfigVo> picConfigMap; private final Map<Long, PicConfigVo> picConfigMap;
/** key: captionSetId */ /** key: captionSetId */
private final Map<Long, List<CaptionVo>> captionSetMap; private final Map<Long, List<CaptionVo>> captionSetMap;
private String config;
public Generator(Long storageConfigId) { public Generator(Long storageConfigId) {
StorageConfigService storageConfigService = SpringUtil.getBean(StorageConfigService.class); Generator generator = CACHE.get(storageConfigId);
PicConfigService picConfigService = SpringUtil.getBean(PicConfigService.class); if (generator == null) {
CaptionService captionService = SpringUtil.getBean(CaptionService.class); StorageConfigService storageConfigService = SpringUtil.getBean(StorageConfigService.class);
PicConfigService picConfigService = SpringUtil.getBean(PicConfigService.class);
StorageConfig storageConfig0 = storageConfigService.getById(storageConfigId); CaptionService captionService = SpringUtil.getBean(CaptionService.class);
this.contexts = StorageConfigVo.copy(storageConfig0).getContexts();
if (contexts == null || contexts.size() == 0) { StorageConfig storageConfig0 = storageConfigService.getById(storageConfigId);
throw new RuntimeException("无法构建改配置"); this.contexts = StorageConfigVo.copy(storageConfig0).getContexts();
} if (contexts == null || contexts.size() == 0) {
throw new RuntimeException("无法构建改配置");
}
List<Long> picConfigIds = contexts.stream().map(Context::getPicConfigId).collect(Collectors.toList()); List<Long> picConfigIds = contexts.stream().map(Context::getPicConfigId).collect(Collectors.toList());
List<PicConfig> picConfigs = picConfigService.listByIds(picConfigIds); List<PicConfig> picConfigs = picConfigService.listByIds(picConfigIds);
this.picConfigMap = new HashMap<>(picConfigs.size()); this.picConfigMap = new HashMap<>(picConfigs.size());
ArrayList<Long> captionSetIds = new ArrayList<>(); ArrayList<Long> captionSetIds = new ArrayList<>();
for (PicConfig item : picConfigs) { for (PicConfig item : picConfigs) {
PicConfigVo copy = PicConfigVo.copy(item); PicConfigVo copy = PicConfigVo.copy(item);
picConfigMap.put(copy.getId(), copy); picConfigMap.put(copy.getId(), copy);
captionSetIds.add(copy.getCaptionSetId()); captionSetIds.add(copy.getCaptionSetId());
} }
List<Caption> captions = captionService.list(captionService.query().in("caption_set_id", captionSetIds).getWrapper()); List<Caption> captions = captionService.list(captionService.query().in("caption_set_id", captionSetIds).getWrapper());
this.captionSetMap = captions.stream().map(CaptionVo::copy).collect(Collectors.groupingBy(CaptionVo::getCaptionSetId, Collectors.toList())); this.captionSetMap = captions.stream().map(CaptionVo::copy).collect(Collectors.groupingBy(CaptionVo::getCaptionSetId, Collectors.toList()));
CACHE.put(storageConfigId, this);
} else {
this.contexts = generator.contexts;
this.picConfigMap = generator.picConfigMap;
this.captionSetMap = generator.captionSetMap;
this.config = generator.config;
}
} }
public String build() { public String build() {
// 基本上算是固定写法 if (this.config == null) {
Node rootNode = document.getFirstChild(); // 基本上算是固定写法
rootNode.appendChild(createTextNode("FileVersion", FILE_VERSION)); Node rootNode = document.getFirstChild();
Element standard = document.createElement("Standard"); rootNode.appendChild(createTextNode("FileVersion", FILE_VERSION));
rootNode.appendChild(standard); Element standard = document.createElement("Standard");
standard.appendChild(createTextNode("SolutionNum", String.valueOf(contexts.size()))); rootNode.appendChild(standard);
Element switchPathConfig = document.createElement("SwitchPathConfig"); standard.appendChild(createTextNode("SolutionNum", String.valueOf(contexts.size())));
switchPathConfig.appendChild(createTextNode("Flag", "1")); Element switchPathConfig = document.createElement("SwitchPathConfig");
switchPathConfig.appendChild(createTextNode("RootPaths", "/VionData/RecordData/viondata0;/VionData/RecordData/viondata1;/VionData/RecordData/viondata2;/VionData/RecordData/viondata3;")); switchPathConfig.appendChild(createTextNode("Flag", "1"));
switchPathConfig.appendChild(createTextNode("VolumeThreshold", "2048")); switchPathConfig.appendChild(createTextNode("RootPaths", "/VionData/RecordData/viondata0;/VionData/RecordData/viondata1;/VionData/RecordData/viondata2;/VionData/RecordData/viondata3;"));
switchPathConfig.appendChild(createTextNode("CountThreshold", "64")); switchPathConfig.appendChild(createTextNode("VolumeThreshold", "2048"));
standard.appendChild(switchPathConfig); switchPathConfig.appendChild(createTextNode("CountThreshold", "64"));
standard.appendChild(switchPathConfig);
// 开始组装 Solution
for (Context context : contexts) { // 开始组装 Solution
Node solution = buildSolution(context); for (Context context : contexts) {
if (solution != null) { Node solution = buildSolution(context);
standard.appendChild(solution); if (solution != null) {
standard.appendChild(solution);
}
} }
this.config = XmlUtil.toStr(document, "GBK", false);
} }
return XmlUtil.toStr(document, "GBK", false); return this.config;
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!