From 5c0fac7de694bcf005e1a57db09aeb793459eca3 Mon Sep 17 00:00:00 2001 From: Rin <144466399+rin259@users.noreply.github.com> Date: Sun, 22 Mar 2026 12:19:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BE=A4=E5=85=AC=E5=91=8A/=E7=B2=BE?= =?UTF-8?q?=E5=8D=8E=E7=AD=89=E6=8E=A5=E5=8F=A3=E5=BC=95=E5=85=A5=20psKey?= =?UTF-8?q?=20=E7=BC=93=E5=AD=98=20(#1716)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 群公告/精华等接口的 psKey 缓存优化 - 在 NTQQGroupApi 中新增 _qunPskeyCache 缓存字段(TTL 4分钟) - 新增 getQunPskey() 私有方法,统一管理 qun.qq.com 域名的 psKey 获取 - fetchGroupEssenceList、deleteGroupBulletin、uploadGroupBulletinPic、publishGroupBulletin 共 4 处调用改为共享缓存,避免每次请求都独立拉取 psKey,减少 HTTP 请求次数 - 缓存过期后自动刷新,无需手动管理 * fix: 改用滑动 TTL + 惰性删除管理 psKey 缓存 - TTL 从硬编码 4 分钟改为 60 秒滑动窗口 - 每次访问顺延过期时间(滑动 TTL),避免频繁刷新 - 过期后惰性删除,下次访问时直接覆盖,无主动清理开销 - 回应 reviewer 建议:滑动窗口比固定 TTL 更适合偶发调用场景 --------- Co-authored-by: rin --- packages/napcat-core/apis/group.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/napcat-core/apis/group.ts b/packages/napcat-core/apis/group.ts index 2b3c17e95..1aa9f8b0d 100644 --- a/packages/napcat-core/apis/group.ts +++ b/packages/napcat-core/apis/group.ts @@ -24,12 +24,28 @@ export class NTQQGroupApi { core: NapCatCore; groupMemberCache: Map> = new Map>(); essenceLRU = new LimitedHashTable(1000); + // psKey 滑动缓存,TTL 60 秒,惰性删除(不主动清理,等下次访问检测到过期再刷新) + private _qunPskeyCache: { value: string; expireAt: number } | null = null; constructor (context: InstanceContext, core: NapCatCore) { this.context = context; this.core = core; } + /** 获取 qun.qq.com 域名的 psKey,滑动 TTL 60 秒(每次访问顺延) */ + private async getQunPskey (): Promise { + const now = Date.now(); + if (this._qunPskeyCache && this._qunPskeyCache.expireAt > now) { + // 滑动 TTL:每次访问都顺延过期时间 + this._qunPskeyCache.expireAt = now + 60 * 1000; + return this._qunPskeyCache.value; + } + // 过期或为空,惰性删除旧值(直接覆盖)并重新获取 + const psKey = (await this.core.apis.UserApi.getPSkey(['qun.qq.com'])).domainPskeyMap.get('qun.qq.com')!; + this._qunPskeyCache = { value: psKey, expireAt: now + 60 * 1000 }; + return psKey; + } + async setGroupRemark (groupCode: string, remark: string) { return this.context.session.getGroupService().modifyGroupRemark(groupCode, remark); } @@ -75,7 +91,7 @@ export class NTQQGroupApi { } async fetchGroupEssenceList (groupCode: string) { - const pskey = (await this.core.apis.UserApi.getPSkey(['qun.qq.com'])).domainPskeyMap.get('qun.qq.com')!; + const pskey = await this.getQunPskey(); return this.context.session.getGroupService().fetchGroupEssenceList({ groupCode, pageStart: 0, @@ -319,7 +335,7 @@ export class NTQQGroupApi { } async deleteGroupBulletin (groupCode: string, noticeId: string) { - const psKey = (await this.core.apis.UserApi.getPSkey(['qun.qq.com'])).domainPskeyMap.get('qun.qq.com')!; + const psKey = await this.getQunPskey(); return this.context.session.getGroupService().deleteGroupBulletin(groupCode, psKey, noticeId); } @@ -423,7 +439,7 @@ export class NTQQGroupApi { } async uploadGroupBulletinPic (groupCode: string, imageurl: string) { - const _Pskey = (await this.core.apis.UserApi.getPSkey(['qun.qq.com'])).domainPskeyMap.get('qun.qq.com')!; + const _Pskey = await this.getQunPskey(); return this.context.session.getGroupService().uploadGroupBulletinPic(groupCode, _Pskey, imageurl); } @@ -475,7 +491,7 @@ export class NTQQGroupApi { width: number, height: number; } | undefined = undefined, pinned: number = 0, confirmRequired: number = 0) { - const psKey = (await this.core.apis.UserApi.getPSkey(['qun.qq.com'])).domainPskeyMap.get('qun.qq.com'); + const psKey = await this.getQunPskey(); // text是content内容url编码 const data = { text: encodeURI(content),