From 5073124113b631394c49e50bfded195a6692c946 Mon Sep 17 00:00:00 2001 From: LiuJiaNan <15703339975@163.com> Date: Thu, 7 May 2026 14:34:37 +0800 Subject: [PATCH] =?UTF-8?q?perf(components):=20=E4=B8=BA=E5=AD=97=E5=85=B8?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E6=B7=BB=E5=8A=A0=E5=85=A8=E5=B1=80=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 Cascader Dictionary 组件中添加 Map 缓存 - 在 LeftTree Department Gwj 组件中添加 Map 缓存 - 新增 Dictionary LeftTree 组件并实现缓存功能 - 在 Select Dictionary 组件中添加 Map 缓存 - 在 SelectTree Dictionary 组件中添加 Map 缓存 - 使用 JSON.stringify 生成缓存键避免重复请求 - 实现缓存命中直接返回数据提升性能 --- src/components/Cascader/Dictionary/index.js | 16 ++++++++++++++++ .../LeftTree/Department/Gwj/index.js | 19 +++++++++++++++++++ src/components/LeftTree/Dictionary/index.js | 16 ++++++++++++++++ src/components/Select/Dictionary/index.js | 16 ++++++++++++++++ src/components/SelectTree/Dictionary/index.js | 16 ++++++++++++++++ 5 files changed, 83 insertions(+) diff --git a/src/components/Cascader/Dictionary/index.js b/src/components/Cascader/Dictionary/index.js index 5e982b0..ecd1eac 100644 --- a/src/components/Cascader/Dictionary/index.js +++ b/src/components/Cascader/Dictionary/index.js @@ -3,6 +3,9 @@ import { useEffect, useState } from "react"; import { DICTIONARY_APP_KEY_ENUM } from "../../../enum/dictionary"; import BasicCascader from "../Basic"; +// 全局缓存 +const cacheMap = new Map(); + /** * 数据字典级联组件 */ @@ -30,7 +33,20 @@ function DictionaryCascader(props) { return; } + // 生成缓存键 + const cacheKey = JSON.stringify({ appKey, dictValue }); + + // 检查缓存,如果存在直接返回缓存结果 + if (cacheMap.has(cacheKey)) { + setTreeData(cacheMap.get(cacheKey)); + return; + } + const { data } = await request("/config/dict-trees/list/by/dictValues", "get", { appKey, dictValue }); + + // 存入缓存 + cacheMap.set(cacheKey, data); + setTreeData(data); }; diff --git a/src/components/LeftTree/Department/Gwj/index.js b/src/components/LeftTree/Department/Gwj/index.js index 71c6c72..79068b3 100644 --- a/src/components/LeftTree/Department/Gwj/index.js +++ b/src/components/LeftTree/Department/Gwj/index.js @@ -2,6 +2,9 @@ import { request } from "@cqsjjb/jjb-common-lib/http.js"; import { useEffect, useState } from "react"; import BasicLeftTree from "../../Basic"; +// 全局缓存 +const cacheMap = new Map(); + /** * 部门左侧树组件(港务局版本) */ @@ -15,6 +18,18 @@ function DepartmentLeftTree(props) { const [treeData, setTreeData] = useState([]); const getData = async () => { + setTreeData([]); + + // 生成缓存键 + const paramsStr = JSON.stringify(params); + const cacheKey = `${searchType}_${paramsStr}`; + + // 检查缓存,如果存在直接返回缓存结果 + if (cacheMap.has(cacheKey)) { + setTreeData(cacheMap.get(cacheKey)); + return; + } + let requestUrl = ""; const actualParams = { ...params, time: new Date().getTime() }; if (searchType === "current") { @@ -39,6 +54,10 @@ function DepartmentLeftTree(props) { } const { data } = await request(requestUrl, "post", actualParams); + + // 存入缓存 + cacheMap.set(cacheKey, data); + setTreeData(data); }; diff --git a/src/components/LeftTree/Dictionary/index.js b/src/components/LeftTree/Dictionary/index.js index 2cc1088..baf2eb3 100644 --- a/src/components/LeftTree/Dictionary/index.js +++ b/src/components/LeftTree/Dictionary/index.js @@ -3,6 +3,9 @@ import { useEffect, useState } from "react"; import { DICTIONARY_APP_KEY_ENUM } from "../../../enum/dictionary"; import BasicLeftTree from "../Basic"; +// 全局缓存 +const cacheMap = new Map(); + /** * 数据字典左侧树组件 */ @@ -30,7 +33,20 @@ function DictionaryLeftTree(props) { return; } + // 生成缓存键 + const cacheKey = JSON.stringify({ appKey, dictValue }); + + // 检查缓存,如果存在直接返回缓存结果 + if (cacheMap.has(cacheKey)) { + setTreeData(cacheMap.get(cacheKey)); + return; + } + const { data } = await request("/config/dict-trees/list/by/dictValues", "get", { appKey, dictValue }); + + // 存入缓存 + cacheMap.set(cacheKey, data); + setTreeData(data); }; diff --git a/src/components/Select/Dictionary/index.js b/src/components/Select/Dictionary/index.js index 21b41b1..70fcb21 100644 --- a/src/components/Select/Dictionary/index.js +++ b/src/components/Select/Dictionary/index.js @@ -3,6 +3,9 @@ import { useEffect, useState } from "react"; import { DICTIONARY_APP_KEY_ENUM } from "../../../enum/dictionary"; import BasicSelect from "../Basic"; +// 全局缓存 +const cacheMap = new Map(); + /** * 数据字典下拉组件 */ @@ -30,7 +33,20 @@ function DictionarySelect(props) { return; } + // 生成缓存键 + const cacheKey = JSON.stringify({ appKey, dictValue }); + + // 检查缓存,如果存在直接返回缓存结果 + if (cacheMap.has(cacheKey)) { + setData(cacheMap.get(cacheKey)); + return; + } + const { data } = await request("/config/dict-trees/list/by/dictValues", "get", { appKey, dictValue }); + + // 存入缓存 + cacheMap.set(cacheKey, data); + setData(data); }; diff --git a/src/components/SelectTree/Dictionary/index.js b/src/components/SelectTree/Dictionary/index.js index 569d641..3f43edc 100644 --- a/src/components/SelectTree/Dictionary/index.js +++ b/src/components/SelectTree/Dictionary/index.js @@ -3,6 +3,9 @@ import { useEffect, useState } from "react"; import { DICTIONARY_APP_KEY_ENUM } from "../../../enum/dictionary"; import BasicSelectTree from "../Basic"; +// 全局缓存 +const cacheMap = new Map(); + /** * 数据字典下拉树组件 */ @@ -30,7 +33,20 @@ function DictionarySelectTree(props) { return; } + // 生成缓存键 + const cacheKey = JSON.stringify({ appKey, dictValue }); + + // 检查缓存,如果存在直接返回缓存结果 + if (cacheMap.has(cacheKey)) { + setTreeData(cacheMap.get(cacheKey)); + return; + } + const { data } = await request("/config/dict-trees/list/by/dictValues", "get", { appKey, dictValue }); + + // 存入缓存 + cacheMap.set(cacheKey, data); + setTreeData(data); };