XingDy姐

给toke一个缓存,防止多次访问

电脑版发表于:2019/12/11 18:16

Redis 缓存一个toke 代码如下:

 public static string GetToken()
        {
            RedisClient redisClient = new RedisClient();
            // 先从缓存获取
            string toke = redisClient.Get<string>("toke");
            if (toke != null)
            {
                return toke;
            }
            //缓存中没有找到从外网获取 
            HttpClient httpClient = new HttpClient();
            string reuslt = httpClient.GetAsync("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx06d6f658f31d964d&secret=8abb7a5ec4cf3f1af0a6dcb1cdd77678").Result.Content.ReadAsStringAsync().Result;
            AccessTokenToolsDTO accessTokenToolsDTO = JsonConvert.DeserializeObject<AccessTokenToolsDTO>(reuslt);
            // 吧toke写入缓存
            redisClient.Set<string>("toke", accessTokenToolsDTO.access_token,TimeSpan.FromSeconds(accessTokenToolsDTO.expires_in-160));
            return accessTokenToolsDTO.access_token;
        }


关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢