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