<feat>缓存客户端

This commit is contained in:
renzhiyuan 2025-07-07 18:10:22 +08:00
parent 2c4974a25f
commit b2722abfa9
1 changed files with 10 additions and 4 deletions

View File

@ -15,16 +15,22 @@ import (
"github.com/qit-team/snow-core/log/logger" "github.com/qit-team/snow-core/log/logger"
"net/url" "net/url"
"strconv" "strconv"
"sync"
"time" "time"
) )
var WxClientGroup = make(map[string]*wechat.ClientV3) var WxClientGroup sync.Map
func WxClientGroupKey(wxConfig WxPay) string {
return fmt.Sprintf("%s%s", wxConfig.AppId, wxConfig.MchId)
}
// InitClient 使用提供的支付请求参数初始化微信客户端 // InitClient 使用提供的支付请求参数初始化微信客户端
func InitClient(wxConfig WxPay) (*wechat.ClientV3, error) { func InitClient(wxConfig WxPay) (*wechat.ClientV3, error) {
clientCache, ex := WxClientGroup[wxConfig.AppId] key := WxClientGroupKey(wxConfig)
clientCache, ex := WxClientGroup.Load(key)
if ex { if ex {
return clientCache, nil return clientCache.(*wechat.ClientV3), nil
} }
// NewClientV3 初始化微信客户端 v3 // NewClientV3 初始化微信客户端 v3
// mchid商户ID 或者服务商模式的 sp_mchid // mchid商户ID 或者服务商模式的 sp_mchid
@ -51,7 +57,7 @@ func InitClient(wxConfig WxPay) (*wechat.ClientV3, error) {
// 打开Debug开关输出日志默认是关闭的 // 打开Debug开关输出日志默认是关闭的
wxClient.DebugSwitch = gopay.DebugOn wxClient.DebugSwitch = gopay.DebugOn
WxClientGroup[wxConfig.AppId] = wxClient WxClientGroup.Store(key, wxClient)
return wxClient, nil return wxClient, nil
} }