refactor: remove debug

This commit is contained in:
手瓜一十雪 2024-06-19 10:01:24 +08:00
parent 7519825303
commit c72ebe495c

View File

@ -63,15 +63,27 @@ export function simpleDecorator(target: any, context: any) {
// } // }
// } // }
export function CacheClassFuncAsync(ttl: number = 3600 * 1000, customKey: string = '') { 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) { function logExecutionTime(target: any, methodName: string, descriptor: PropertyDescriptor) {
console.log('logExecutionTime', target, methodName, descriptor); //console.log('logExecutionTime', target, methodName, descriptor);
const cache = new Map<string, { expiry: number; value: any }>();
const originalMethod = descriptor.value; const originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) { descriptor.value = async function (...args: any[]) {
const start = Date.now(); const key = `${customKey}${String(methodName)}.(${args.map(arg => JSON.stringify(arg)).join(', ')})`;
const result = originalMethod.apply(this, args); cache.forEach((value, key) => {
const end = Date.now(); if (value.expiry < Date.now()) {
console.log(`Method ${methodName} executed in ${end - start} ms.`); 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; return result;
}; };
} }