From ff1d3366beb80a7a65e55f1ed8987dce1c138bba Mon Sep 17 00:00:00 2001 From: fit2cloud-chenyw Date: Tue, 14 Apr 2026 09:03:37 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=89=8D=E7=AB=AF=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=A2=9E=E5=8A=A0=E5=89=8D=E7=BC=80=E9=81=BF?= =?UTF-8?q?=E5=85=8D=20localStorage-key=20=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/core-frontend/src/hooks/web/useCache.ts | 53 +++++++++++++++++--- core/core-frontend/src/utils/cacheUtil.ts | 4 +- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/core/core-frontend/src/hooks/web/useCache.ts b/core/core-frontend/src/hooks/web/useCache.ts index a762baf5ac..ebeab0a061 100644 --- a/core/core-frontend/src/hooks/web/useCache.ts +++ b/core/core-frontend/src/hooks/web/useCache.ts @@ -1,17 +1,54 @@ -/** - * 配置浏览器本地存储的方式,可直接存储对象数组。 - */ - +// utils/cache.ts import WebStorageCache from 'web-storage-cache' -type CacheType = 'sessionStorage' | 'localStorage' +type CacheType = 'localStorage' | 'sessionStorage' + +const getPathPrefix = () => { + const pathname = window.location.pathname + const match = pathname.match(/^\/([^\/]+)/) + return match ? `${match[1]}_` : 'de_v2_' +} export const useCache = (type: CacheType = 'localStorage') => { - const wsCache: WebStorageCache = new WebStorageCache({ - storage: type + const originalCache = new WebStorageCache({ storage: type }) + const prefix = getPathPrefix() + + const shouldAddPrefix = (key: string): boolean => { + return key.startsWith('user.') + } + + const processKey = (key: string): string => { + return shouldAddPrefix(key) ? `${prefix}${key}` : key + } + + const methodsNeedKeyPrefix = new Set(['get', 'delete', 'touch', 'add', 'replace']) + + const wrappedCache = new Proxy(originalCache, { + get(target, prop, receiver) { + const originalMethod = target[prop as keyof typeof target] + + if (typeof originalMethod !== 'function') { + return originalMethod + } + + if (methodsNeedKeyPrefix.has(prop as string)) { + return function (this: any, key: string, ...args: any[]) { + const processedKey = processKey(key) + return originalMethod.call(target, processedKey, ...args) + } + } + + if (prop === 'set') { + return function (this: any, key: string, value: any, ...args: any[]) { + const processedKey = processKey(key) + return originalMethod.call(target, processedKey, value, ...args) + } + } + return originalMethod.bind(target) + } }) return { - wsCache + wsCache: wrappedCache } } diff --git a/core/core-frontend/src/utils/cacheUtil.ts b/core/core-frontend/src/utils/cacheUtil.ts index f37de37b6a..5d59b23605 100644 --- a/core/core-frontend/src/utils/cacheUtil.ts +++ b/core/core-frontend/src/utils/cacheUtil.ts @@ -1,3 +1,5 @@ +import { useCache } from '@/hooks/web/useCache' +const { wsCache } = useCache() export const clearCache = () => { const keys = [ 'DataEaseKey', @@ -16,6 +18,6 @@ export const clearCache = () => { 'user.uid' ] keys.forEach(key => { - localStorage.removeItem(key) + wsCache.delete(key) }) }