From 9836d6d9bde266c1e79333d505e1f218567e7ffe Mon Sep 17 00:00:00 2001
From: 月夜 <768242801@qq.com>
Date: 星期一, 11 十一月 2024 15:10:24 +0800
Subject: [PATCH] 优化:字典缓存使用Map代替Array,更高效

---
 src/store/modules/dict.ts |  125 ++++++++++++++++++-----------------------
 1 files changed, 56 insertions(+), 69 deletions(-)

diff --git a/src/store/modules/dict.ts b/src/store/modules/dict.ts
index 50b8871..cd1a41d 100644
--- a/src/store/modules/dict.ts
+++ b/src/store/modules/dict.ts
@@ -1,78 +1,65 @@
 export const useDictStore = defineStore('dict', () => {
-	const dict = ref<
-		Array<{
-			key: string;
-			value: DictDataOption[];
-		}>
-	>([]);
+  const dict = ref<Map<string, DictDataOption[]>>(new Map());
 
-	/**
-	 * 鑾峰彇瀛楀吀
-	 * @param _key 瀛楀吀key
-	 */
-	const getDict = (_key: string): DictDataOption[] | null => {
-		if (_key == null && _key == '') {
-			return null;
-		}
-		try {
-			for (let i = 0; i < dict.value.length; i++) {
-				if (dict.value[i].key == _key) {
-					return dict.value[i].value;
-				}
-			}
-		} catch (e) {
-			return null;
-		}
-		return null;
-	};
+  /**
+   * 鑾峰彇瀛楀吀
+   * @param _key 瀛楀吀key
+   */
+  const getDict = (_key: string): DictDataOption[] | null => {
+    if (!_key) {
+      return null;
+    }
+    return dict.value.get(_key) || null;
+  };
 
-	/**
-	 * 璁剧疆瀛楀吀
-	 * @param _key 瀛楀吀key
-	 * @param _value 瀛楀吀value
-	 */
-	const setDict = (_key: string, _value: DictDataOption[]) => {
-		if (_key !== null && _key !== '') {
-			dict.value.push({
-				key: _key,
-				value: _value
-			});
-		}
-	};
+  /**
+   * 璁剧疆瀛楀吀
+   * @param _key 瀛楀吀key
+   * @param _value 瀛楀吀value
+   */
+  const setDict = (_key: string, _value: DictDataOption[]) => {
+    if (!_key) {
+      return false;
+    }
+    try {
+      dict.value.set(_key, _value);
+      return true;
+    } catch (e) {
+      console.error('Error in setDict:', e);
+      return false;
+    }
+  };
 
-	/**
-	 * 鍒犻櫎瀛楀吀
-	 * @param _key
-	 */
-	const removeDict = (_key: string): boolean => {
-		let bln = false;
-		try {
-			for (let i = 0; i < dict.value.length; i++) {
-				if (dict.value[i].key == _key) {
-					dict.value.splice(i, 1);
-					return true;
-				}
-			}
-		} catch (e) {
-			bln = false;
-		}
-		return bln;
-	};
+  /**
+   * 鍒犻櫎瀛楀吀
+   * @param _key
+   */
+  const removeDict = (_key: string): boolean => {
+    if (!_key) {
+      return false;
+    }
+    try {
+      return dict.value.delete(_key);
+    } catch (e) {
+      console.error('Error in removeDict:', e);
+      return false;
+    }
+  };
 
-	/**
-	 * 娓呯┖瀛楀吀
-	 */
-	const cleanDict = (): void => {
-		dict.value = [];
-	};
+  /**
+   * 娓呯┖瀛楀吀
+   */
+  const cleanDict = (): void => {
+    dict.value.clear();
+  };
 
-	return {
-		dict,
-		getDict,
-		setDict,
-		removeDict,
-		cleanDict
-	};
+  return {
+    dict,
+    getDict,
+    setDict,
+    removeDict,
+    cleanDict
+  };
 });
 
 export default useDictStore;

--
Gitblit v1.9.3