mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
refactor: test
This commit is contained in:
parent
d9315bf309
commit
7519825303
@ -62,40 +62,57 @@ export function simpleDecorator(target: any, context: any) {
|
|||||||
// return CacheClassFuncDecoratorInternal;
|
// return CacheClassFuncDecoratorInternal;
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
export function CacheClassFuncAsync(ttl: number = 3600 * 1000, customKey: string = '') {
|
||||||
export function CacheClassFuncAsync(ttl: number = 3600 * 1000, customKey: string = ''): any {
|
console.log('CacheClassFuncAsync', ttl, customKey);
|
||||||
const cache = new Map<string, { expiry: number; value: any }>();
|
function logExecutionTime(target: any, methodName: string, descriptor: PropertyDescriptor) {
|
||||||
|
console.log('logExecutionTime', target, methodName, descriptor);
|
||||||
// 注意:在JavaScript装饰器中,我们通常不直接处理ClassMethodDecoratorContext这样的类型,
|
const originalMethod = descriptor.value;
|
||||||
// 因为装饰器的参数通常是目标类(对于类装饰器)、属性名(对于属性装饰器)等。
|
descriptor.value = function (...args: any[]) {
|
||||||
// 对于方法装饰器,我们关注的是方法本身及其描述符。
|
const start = Date.now();
|
||||||
// 但这里我们维持原逻辑,假设有一个自定义的处理上下文的方式。
|
const result = originalMethod.apply(this, args);
|
||||||
|
const end = Date.now();
|
||||||
return function (originalMethod: Function): any {
|
console.log(`Method ${methodName} executed in ${end - start} ms.`);
|
||||||
// 由于JavaScript装饰器原生不支持异步直接定义,我们保持async定义以便处理异步方法。
|
|
||||||
async function decoratorWrapper(this: any, ...args: any[]): Promise<any> {
|
|
||||||
const key = `${customKey}${originalMethod.name}.(${args.map(arg => JSON.stringify(arg)).join(', ')})`;
|
|
||||||
const cachedValue = cache.get(key);
|
|
||||||
// 遍历cache 清除expiry内容
|
|
||||||
cache.forEach((value, key) => {
|
|
||||||
if (value.expiry < Date.now()) {
|
|
||||||
cache.delete(key);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (cachedValue && cachedValue.expiry > Date.now()) {
|
|
||||||
return cachedValue.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 直接await异步方法的结果
|
|
||||||
const result = await originalMethod.apply(this, args);
|
|
||||||
cache.set(key, { expiry: Date.now() + ttl, value: result });
|
|
||||||
return result;
|
return result;
|
||||||
}
|
|
||||||
|
|
||||||
// 返回装饰后的方法,保持与原方法相同的名称和描述符(如果需要更精细的控制,可以考虑使用Object.getOwnPropertyDescriptor等)
|
|
||||||
return decoratorWrapper;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
return logExecutionTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
// export function CacheClassFuncAsync(ttl: number = 3600 * 1000, customKey: string = ''): any {
|
||||||
|
// const cache = new Map<string, { expiry: number; value: any }>();
|
||||||
|
|
||||||
|
// // 注意:在JavaScript装饰器中,我们通常不直接处理ClassMethodDecoratorContext这样的类型,
|
||||||
|
// // 因为装饰器的参数通常是目标类(对于类装饰器)、属性名(对于属性装饰器)等。
|
||||||
|
// // 对于方法装饰器,我们关注的是方法本身及其描述符。
|
||||||
|
// // 但这里我们维持原逻辑,假设有一个自定义的处理上下文的方式。
|
||||||
|
|
||||||
|
// return function (originalMethod: Function): any {
|
||||||
|
// console.log(originalMethod);
|
||||||
|
// // 由于JavaScript装饰器原生不支持异步直接定义,我们保持async定义以便处理异步方法。
|
||||||
|
// async function decoratorWrapper(this: any, ...args: any[]): Promise<any> {
|
||||||
|
// console.log(...args);
|
||||||
|
// const key = `${customKey}${originalMethod.name}.(${args.map(arg => JSON.stringify(arg)).join(', ')})`;
|
||||||
|
// const cachedValue = cache.get(key);
|
||||||
|
// // 遍历cache 清除expiry内容
|
||||||
|
// cache.forEach((value, key) => {
|
||||||
|
// if (value.expiry < Date.now()) {
|
||||||
|
// cache.delete(key);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// if (cachedValue && cachedValue.expiry > Date.now()) {
|
||||||
|
// return cachedValue.value;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // 直接await异步方法的结果
|
||||||
|
// const result = await originalMethod.apply(this, args);
|
||||||
|
// cache.set(key, { expiry: Date.now() + ttl, value: result });
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // 返回装饰后的方法,保持与原方法相同的名称和描述符(如果需要更精细的控制,可以考虑使用Object.getOwnPropertyDescriptor等)
|
||||||
|
// return decoratorWrapper;
|
||||||
|
// };
|
||||||
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 函数缓存装饰器,根据方法名、参数、自定义key生成缓存键,在一定时间内返回缓存结果
|
* 函数缓存装饰器,根据方法名、参数、自定义key生成缓存键,在一定时间内返回缓存结果
|
||||||
|
@ -40,7 +40,7 @@ if (process.env.NAPCAT_BUILDSYS == 'linux') {
|
|||||||
const baseConfigPlugin: PluginOption[] = [
|
const baseConfigPlugin: PluginOption[] = [
|
||||||
// PreprocessorDirectives(),
|
// PreprocessorDirectives(),
|
||||||
babel({
|
babel({
|
||||||
filter: /.*\.(ts)$/,
|
filter: /.*\.(ts|js)$/,
|
||||||
babelConfig: {
|
babelConfig: {
|
||||||
babelrc: false,
|
babelrc: false,
|
||||||
configFile: false,
|
configFile: false,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user