From c72ebe495cb47cd308b4a6bd43537ac8422c6fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Wed, 19 Jun 2024 10:01:24 +0800 Subject: [PATCH] refactor: remove debug --- src/common/utils/helper.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/common/utils/helper.ts b/src/common/utils/helper.ts index 5a984ff3..a2bfb0a1 100644 --- a/src/common/utils/helper.ts +++ b/src/common/utils/helper.ts @@ -63,15 +63,27 @@ export function simpleDecorator(target: any, context: any) { // } // } export function CacheClassFuncAsync(ttl: number = 3600 * 1000, customKey: string = '') { - console.log('CacheClassFuncAsync', ttl, customKey); + //console.log('CacheClassFuncAsync', ttl, customKey); function logExecutionTime(target: any, methodName: string, descriptor: PropertyDescriptor) { - console.log('logExecutionTime', target, methodName, descriptor); + //console.log('logExecutionTime', target, methodName, descriptor); + const cache = new Map(); const originalMethod = descriptor.value; - descriptor.value = function (...args: any[]) { - const start = Date.now(); - const result = originalMethod.apply(this, args); - const end = Date.now(); - console.log(`Method ${methodName} executed in ${end - start} ms.`); + descriptor.value = async function (...args: any[]) { + const key = `${customKey}${String(methodName)}.(${args.map(arg => JSON.stringify(arg)).join(', ')})`; + cache.forEach((value, key) => { + if (value.expiry < Date.now()) { + cache.delete(key); + } + }); + const cachedValue = cache.get(key); + if (cachedValue && cachedValue.expiry > Date.now()) { + return cachedValue.value; + } + // const start = Date.now(); + const result = await originalMethod.apply(this, args); + // const end = Date.now(); + // console.log(`Method ${methodName} executed in ${end - start} ms.`); + cache.set(key, { expiry: Date.now() + ttl, value: result }); return result; }; }