From 03fc7dc5d536f08e711f7ddb79f720c2e1ded16b Mon Sep 17 00:00:00 2001 From: yangys <y_ys79@sina.com> Date: 星期日, 13 四月 2025 21:59:12 +0800 Subject: [PATCH] 增加状态核实;增加模板是否set的缓存优化 --- collect/src/main/java/com/qianwen/mdc/collect/service/IotDBCommonService.java | 34 +++++++++++++++++++++++++++++++++- 1 files changed, 33 insertions(+), 1 deletions(-) diff --git a/collect/src/main/java/com/qianwen/mdc/collect/service/IotDBCommonService.java b/collect/src/main/java/com/qianwen/mdc/collect/service/IotDBCommonService.java index 9984718..f6866fe 100644 --- a/collect/src/main/java/com/qianwen/mdc/collect/service/IotDBCommonService.java +++ b/collect/src/main/java/com/qianwen/mdc/collect/service/IotDBCommonService.java @@ -1,6 +1,7 @@ package com.qianwen.mdc.collect.service; import java.util.List; +import java.util.concurrent.TimeUnit; import org.apache.iotdb.rpc.IoTDBConnectionException; import org.apache.iotdb.rpc.StatementExecutionException; @@ -9,6 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; import com.qianwen.mdc.collect.config.IotDBSessionConfig; @@ -23,12 +26,27 @@ private IotDBSessionConfig iotdbCfg; /** + * 缂撳瓨绉掓暟 + */ + public static final int CACHE_DURATION_SEC = 300;//5mins + /** + * 妯℃澘鏄惁璁剧疆鐨勭紦瀛� + */ + Cache<String, Boolean> templateSetCache; + + public IotDBCommonService() { + templateSetCache = Caffeine.newBuilder() + // 鏈�澶ize锛岃秴杩囧垯鏀惧純鑰佺殑,澶ф姣忎釜鏁版嵁鐐归兘搴旇鏈変竴涓紦瀛橈紝澶栧姞涓�浜涘叾浠栫被鍨嬬殑琛紙鍏tate_xxx锛� + .maximumSize(5000).expireAfterWrite(CACHE_DURATION_SEC, TimeUnit.SECONDS) + .build(); + } + /** * 妯℃澘鏄惁鎸傝浇鍒版寚瀹氳矾寰勪簡 * @param template 妯℃澘鍚嶇О * @param path 璁惧璺緞 * @return */ - public boolean isTemplateSetOnPath(String template,String path) { + private boolean isTemplateSetOnPath(String template,String path) { List<String> pathlist; try { pathlist = iotdbCfg.getSessionPool().showPathsTemplateSetOn(template); @@ -42,14 +60,28 @@ } /** + * 妫�鏌ュ湪缂撳瓨涓澶噄d鏄惁琚缃繃 + * @param path + * @return + */ + private boolean isTemplateSetInCache(String path) { + return templateSetCache.getIfPresent(path) != null;//涓嶇敤鑰冭檻true/false,鏈塳ey灏辨槸璁剧疆杩� + } + /** * 濡傛灉deviceId鎸囧畾鐨勮矾寰勬病鏈夋寕杞芥ā鏉垮垯鎵ц鎸傝浇 * @param template 妯℃澘鍚嶇О * @param deviceId 璁惧璺緞 */ public void setTemmplateIfNotSet(String template,String deviceId) { + if(isTemplateSetInCache(deviceId)) {//缂撳瓨涓褰曡繃锛岃鏄庡凡缁忚缃ā鏉夸簡锛屼笉鍐嶇户缁墽琛屽悗缁楠� + return; + } + if(!isTemplateSetOnPath(template, deviceId)) { try { iotdbCfg.getSessionPool().setSchemaTemplate(template, deviceId); + //鍦ㄧ紦瀛樹腑璁板綍璇ヨ澶噋ath宸茬粡琚厤缃繃浜� + templateSetCache.put(deviceId, true); } catch (Exception e) { logger.error("涓篸eviceId璁剧疆妯℃澘閿欒,template="+template+",deviceId="+deviceId,e); } -- Gitblit v1.9.3