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