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,8 +46,11 @@ 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) {
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);
......@@ -69,9 +75,17 @@ public class Generator {
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()));
CACHE.put(storageConfigId, this);
} else {
this.contexts = generator.contexts;
this.picConfigMap = generator.picConfigMap;
this.captionSetMap = generator.captionSetMap;
this.config = generator.config;
}
}
public String build() {
if (this.config == null) {
// 基本上算是固定写法
Node rootNode = document.getFirstChild();
rootNode.appendChild(createTextNode("FileVersion", FILE_VERSION));
......@@ -92,7 +106,9 @@ public class Generator {
standard.appendChild(solution);
}
}
return XmlUtil.toStr(document, "GBK", false);
this.config = 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!