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 | 55 +++++++++++++++++++++----------------------------------
1 files changed, 21 insertions(+), 34 deletions(-)
diff --git a/src/store/modules/dict.ts b/src/store/modules/dict.ts
index 2f937b9..cd1a41d 100644
--- a/src/store/modules/dict.ts
+++ b/src/store/modules/dict.ts
@@ -1,29 +1,15 @@
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 == '') {
+ if (!_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;
+ return dict.value.get(_key) || null;
};
/**
@@ -32,11 +18,15 @@
* @param _value 瀛楀吀value
*/
const setDict = (_key: string, _value: DictDataOption[]) => {
- if (_key !== null && _key !== '') {
- dict.value.push({
- key: _key,
- value: _value
- });
+ if (!_key) {
+ return false;
+ }
+ try {
+ dict.value.set(_key, _value);
+ return true;
+ } catch (e) {
+ console.error('Error in setDict:', e);
+ return false;
}
};
@@ -45,25 +35,22 @@
* @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;
+ if (!_key) {
+ return false;
}
- return bln;
+ try {
+ return dict.value.delete(_key);
+ } catch (e) {
+ console.error('Error in removeDict:', e);
+ return false;
+ }
};
/**
* 娓呯┖瀛楀吀
*/
const cleanDict = (): void => {
- dict.value = [];
+ dict.value.clear();
};
return {
--
Gitblit v1.9.3