From 49c4cfaf54a0d85070226d6b721976dd476c21ab Mon Sep 17 00:00:00 2001 From: markwang <2951177317@qq.com> Date: Sun, 24 Sep 2023 10:47:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:GetAccessTokenContext=E4=BB=8Ecache?= =?UTF-8?q?=E4=B8=AD=E8=8E=B7=E5=8F=96=E5=AD=97=E7=AC=A6=E7=AA=9C=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA=E6=97=B6,=E4=BB=8E=E5=BE=AE=E4=BF=A1=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E8=8E=B7=E5=8F=96=20(#721)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: markwang --- credential/default_access_token.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/credential/default_access_token.go b/credential/default_access_token.go index 25416e7c7..90dffd1eb 100644 --- a/credential/default_access_token.go +++ b/credential/default_access_token.go @@ -66,8 +66,9 @@ func (ak *DefaultAccessToken) GetAccessToken() (accessToken string, err error) { func (ak *DefaultAccessToken) GetAccessTokenContext(ctx context.Context) (accessToken string, err error) { // 先从cache中取 accessTokenCacheKey := fmt.Sprintf("%s_access_token_%s", ak.cacheKeyPrefix, ak.appID) - if val := ak.cache.Get(accessTokenCacheKey); val != nil { - return val.(string), nil + val := ak.cache.Get(accessTokenCacheKey) + if accessToken = val.(string); accessToken != "" { + return } // 加上lock,是为了防止在并发获取token时,cache刚好失效,导致从微信服务器上获取到不同token @@ -75,8 +76,9 @@ func (ak *DefaultAccessToken) GetAccessTokenContext(ctx context.Context) (access defer ak.accessTokenLock.Unlock() // 双检,防止重复从微信服务器获取 - if val := ak.cache.Get(accessTokenCacheKey); val != nil { - return val.(string), nil + val = ak.cache.Get(accessTokenCacheKey) + if accessToken = val.(string); accessToken != "" { + return } // cache失效,从微信服务器获取