Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 28, 2025

WxCpMsgAuditServiceImpl.getChatDatas() 每次调用都执行 Finance.NewSdk()Finance.Init(),导致接口超限。根据企微文档,SDK 初始化后有效期为 7200 秒,无需重复初始化。

修改内容

WxCpConfigStorage 接口

新增 SDK 缓存方法:

  • getMsgAuditSdk() / isMsgAuditSdkExpired() - 获取和检查 SDK 状态
  • updateMsgAuditSdk(long sdk, int expiresInSeconds) - 更新缓存
  • expireMsgAuditSdk() - 强制过期

WxCpDefaultConfigImpl / WxCpRedisConfigImpl

实现基于内存的 SDK 缓存(SDK 是 JVM 本地变量,无法通过 Redis 共享)

WxCpMsgAuditServiceImpl

  • 抽取 initSdk() 方法,优先返回缓存的 SDK
  • SDK 缓存 7000 秒(7200 - 200 缓冲)
  • 移除错误处理中多余的 Finance.DestroySdk() 调用

使用示例

// 多次调用 getChatDatas 将复用同一个 SDK 实例
for (int i = 0; i < 100; i++) {
    WxCpChatDatas chatDatas = cpService.getMsgAuditService()
        .getChatDatas(seq, 10L, null, null, 1000L);
    // SDK 已缓存,不会重复初始化
}
// 用户仍可手动销毁
Finance.DestroySdk(chatDatas.getSdk());
Original prompt

This section details on the original issue you should resolve

<issue_title>会话存档-token没有缓存</issue_title>
<issue_description>### 简要描述
me.chanjar.weixin.cp.api.impl.WxCpMsgAuditServiceImpl#getChatDatas
每次调用都会调用long sdk = Finance.NewSdk();以及Finance.Init()方法。实际只需要调用一次,7200s过期。过多调用会导致接口超限问题。
详见企微文档:https://developer.work.weixin.qq.com/document/path/91552

模块版本情况

  • WxJava 模块名: wx-java-cp
  • WxJava 版本号: 4.7.8.B
    </issue_description>

Comments on the Issue (you are @copilot in this section)

Custom agent used: 自定义的
需要用中文


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix caching issue for chat data retrieval 修复会话存档SDK重复初始化导致接口超限问题 Nov 28, 2025
Copilot AI requested a review from binarywang November 28, 2025 02:12
@binarywang binarywang marked this pull request as ready for review November 28, 2025 03:19
@binarywang binarywang merged commit 1da6cf5 into develop Nov 28, 2025
1 check passed
@binarywang binarywang added this to the 4.7.9 milestone Nov 28, 2025
@binarywang binarywang deleted the copilot/fix-chat-datas-cache-issue branch November 28, 2025 03:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

会话存档-token没有缓存

2 participants