From 6fa50c58d32f9021d8460333a93fdaabd4eb7c6e 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, 17 Jul 2024 15:03:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E9=80=9F=E5=BA=A6=20=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E9=A2=91=E7=B9=81=E8=AF=BB=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/helper.ts | 31 ++++++++++++++++++++++++++++++- src/core/src/apis/user.ts | 6 ++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/common/utils/helper.ts b/src/common/utils/helper.ts index 66cb87a6..c7fcbff0 100644 --- a/src/common/utils/helper.ts +++ b/src/common/utils/helper.ts @@ -89,7 +89,36 @@ export function CacheClassFuncAsync(ttl: number = 3600 * 1000, customKey: string } return logExecutionTime; } - +export function CacheClassFuncAsyncExtend(ttl: number = 3600 * 1000, customKey: string = '', checker: any = (_data: any) => { return true; }) { + //console.log('CacheClassFuncAsync', ttl, customKey); + function logExecutionTime(target: any, methodName: string, descriptor: PropertyDescriptor) { + //console.log('logExecutionTime', target, methodName, descriptor); + const cache = new Map(); + const originalMethod = descriptor.value; + 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); + if (!checker(result)) { + return result;//丢弃缓存 + } + // 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 logExecutionTime; +} // export function CacheClassFuncAsync(ttl: number = 3600 * 1000, customKey: string = ''): any { // const cache = new Map(); diff --git a/src/core/src/apis/user.ts b/src/core/src/apis/user.ts index 747feec1..bd9e8e27 100644 --- a/src/core/src/apis/user.ts +++ b/src/core/src/apis/user.ts @@ -1,6 +1,6 @@ import { ModifyProfileParams, SelfInfo, User, UserDetailInfoByUin } from '@/core/entities'; import { friends, selfInfo } from '@/core/data'; -import { CacheClassFuncAsync } from '@/common/utils/helper'; +import { CacheClassFuncAsync, CacheClassFuncAsyncExtend } from '@/common/utils/helper'; import { GeneralCallResult, napCatCore, NTQQFriendApi } from '@/core'; import { ProfileListener } from '@/core/listeners'; import { rejects } from 'assert'; @@ -163,6 +163,7 @@ export class NTQQUserApi { } return skey; } + @CacheClassFuncAsyncExtend(3600, 'Uin2Uid', (data: string | undefined) => { if (data && data.indexOf('u_') != -1) { return true } return false; }) static async getUidByUin(Uin: string) { let ret = await NTEventDispatch.CallNoListenerEvent <(Uin: string[]) => Promise<{ uidInfo: Map }>>( @@ -195,6 +196,7 @@ export class NTQQUserApi { } return uid; } + @CacheClassFuncAsyncExtend(3600, 'Uid2Uin', (data: number | undefined) => { if (data && data != 0 && !isNaN(data)) { return true } return false; }) static async getUinByUid(Uid: string | undefined) { if (!Uid) { return ''; @@ -217,7 +219,7 @@ export class NTQQUserApi { if (!uin) { uin = (await NTQQUserApi.getUserDetailInfo(Uid)).uin; //从QQ Native 转换 } - + // if (!uin) { // uin = (await NTQQFriendApi.getFriends(false)).find((t) => { t.uid == Uid })?.uin; //从QQ Native 缓存转换 // }