Commit 8033818e by xmh

缓存

1 parent 1b130e8f
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.lang.Pair;
import cn.hutool.core.util.XmlUtil;
......@@ -21,6 +23,7 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;
......@@ -35,7 +38,7 @@ import java.util.stream.Collectors;
public class Generator {
private static final String ROOT_ELEMENT_NAME = "StorageConfig";
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);
/** 来自 StorageConfig, 对应 Solution */
private final List<Context> contexts;
......@@ -43,56 +46,69 @@ public class Generator {
private final Map<Long, PicConfigVo> picConfigMap;
/** key: captionSetId */
private final Map<Long, List<CaptionVo>> captionSetMap;
private String config;
public Generator(Long storageConfigId) {
StorageConfigService storageConfigService = SpringUtil.getBean(StorageConfigService.class);
PicConfigService picConfigService = SpringUtil.getBean(PicConfigService.class);
CaptionService captionService = SpringUtil.getBean(CaptionService.class);
StorageConfig storageConfig0 = storageConfigService.getById(storageConfigId);
this.contexts = StorageConfigVo.copy(storageConfig0).getContexts();
if (contexts == null || contexts.size() == 0) {
throw new RuntimeException("无法构建改配置");
}
Generator generator = CACHE.get(storageConfigId);
if (generator == null) {
StorageConfigService storageConfigService = SpringUtil.getBean(StorageConfigService.class);
PicConfigService picConfigService = SpringUtil.getBean(PicConfigService.class);
CaptionService captionService = SpringUtil.getBean(CaptionService.class);
StorageConfig storageConfig0 = storageConfigService.getById(storageConfigId);
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);
this.picConfigMap = new HashMap<>(picConfigs.size());
ArrayList<Long> captionSetIds = new ArrayList<>();
for (PicConfig item : picConfigs) {
PicConfigVo copy = PicConfigVo.copy(item);
picConfigMap.put(copy.getId(), copy);
captionSetIds.add(copy.getCaptionSetId());
}
List<PicConfig> picConfigs = picConfigService.listByIds(picConfigIds);
this.picConfigMap = new HashMap<>(picConfigs.size());
ArrayList<Long> captionSetIds = new ArrayList<>();
for (PicConfig item : picConfigs) {
PicConfigVo copy = PicConfigVo.copy(item);
picConfigMap.put(copy.getId(), copy);
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() {
// 基本上算是固定写法
Node rootNode = document.getFirstChild();
rootNode.appendChild(createTextNode("FileVersion", FILE_VERSION));
Element standard = document.createElement("Standard");
rootNode.appendChild(standard);
standard.appendChild(createTextNode("SolutionNum", String.valueOf(contexts.size())));
Element switchPathConfig = document.createElement("SwitchPathConfig");
switchPathConfig.appendChild(createTextNode("Flag", "1"));
switchPathConfig.appendChild(createTextNode("RootPaths", "/VionData/RecordData/viondata0;/VionData/RecordData/viondata1;/VionData/RecordData/viondata2;/VionData/RecordData/viondata3;"));
switchPathConfig.appendChild(createTextNode("VolumeThreshold", "2048"));
switchPathConfig.appendChild(createTextNode("CountThreshold", "64"));
standard.appendChild(switchPathConfig);
// 开始组装 Solution
for (Context context : contexts) {
Node solution = buildSolution(context);
if (solution != null) {
standard.appendChild(solution);
if (this.config == null) {
// 基本上算是固定写法
Node rootNode = document.getFirstChild();
rootNode.appendChild(createTextNode("FileVersion", FILE_VERSION));
Element standard = document.createElement("Standard");
rootNode.appendChild(standard);
standard.appendChild(createTextNode("SolutionNum", String.valueOf(contexts.size())));
Element switchPathConfig = document.createElement("SwitchPathConfig");
switchPathConfig.appendChild(createTextNode("Flag", "1"));
switchPathConfig.appendChild(createTextNode("RootPaths", "/VionData/RecordData/viondata0;/VionData/RecordData/viondata1;/VionData/RecordData/viondata2;/VionData/RecordData/viondata3;"));
switchPathConfig.appendChild(createTextNode("VolumeThreshold", "2048"));
switchPathConfig.appendChild(createTextNode("CountThreshold", "64"));
standard.appendChild(switchPathConfig);
// 开始组装 Solution
for (Context context : contexts) {
Node solution = buildSolution(context);
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!