tnblog
首页
视频
资源
登录

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

3846人阅读 2019/12/11 18:16 总访问:37347 评论:0 收藏:0 手机
分类: 微信公众号

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;
        }


评价
黑发不知勤学早,白发方悔读书迟......
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术