欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

细说 ASP.NET Cache 及其高级用法

程序员文章站 2022-05-23 21:20:36
持续更新 Cache记录 推荐博客:细说 ASP.NET Cache 及其高级用法 1 #region 缓存 2 3 /// 4 /// 设置Cache 5 /// 6 /// cache名称 7 /// ......

持续更新

 

cache记录  

推荐博客:细说 asp.net cache 及其高级用法

细说 ASP.NET Cache 及其高级用法
 1      #region 缓存
 2 
 3         /// <summary>
 4         /// 设置cache
 5         /// </summary>
 6         /// <param name="key">cache名称</param>
 7         /// <param name="value">cache值</param>
 8         void setcachedata(string key, string value,datetime time)
 9         {
10             cache cache = httpcontext.current.cache;
11             cache["test"] = 1;
12             cache.insert(key, value, null, time, cache.noslidingexpiration);
13         }
14 
15         /// <summary>
16         ///  获取 cache
17         /// </summary>
18         /// <param name="key">cache值</param>
19         /// <returns></returns>
20         object getcachedata(string key)
21         {
22             cache cache = httpruntime.cache;
23             if (cache[key] != null)
24             {
25                 return cache[key];
26             }
27             else
28             {
29                 return "";
30             }
31         }
32 
33         /// <summary>
34         /// 判断指定cache是否存在
35         /// </summary>
36         /// <param name="key"></param>
37         /// <returns></returns>
38         bool iscacheexits(string key)
39         {
40             cache s = httpruntime.cache;
41             return s[key] != null;
42         }
43 
44         /// <summary>
45         /// 移除指定cache
46         /// </summary>
47         /// <param name="key">cache名称</param>
48         void removecachedata(string key)
49         {
50             cache cache = httpruntime.cache;
51             cache.remove(key);
52         }
53 
54         /// <summary>
55         /// 移除所有cache
56         /// </summary>
57         void removeallcachedata()
58         {
59             cache cache = httpruntime.cache;
60             idictionaryenumerator cacheneunm = cache.getenumerator();
61             while (cacheneunm.movenext())
62             {
63                 cache.remove(cacheneunm.key.tostring());
64             }
65         }
66         #endregion
cache

 

获取微信access_token

细说 ASP.NET Cache 及其高级用法
 1  class wxresult
 2     {
 3         public string access_token { get; set; }
 4         public string expires_in { get; set; }
 5     }
 6 
 7 
 8         /// <summary>
 9         /// 缓存的是整个微信返回的json,可以转换成对象获取access_token
10         /// </summary>
11         /// <returns></returns>
12         string  getaccesstoken()
13         {
14             var appid = "";
15             var appresect = "";
16             //判断access_token是否存在
17             if (iscacheexits("access_token"))
18             {
19                 return getcachedata("access_token").tostring();
20             }
21             else
22             {
23                 var url =
24                     $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={appresect}";
25                 var accesstoken = httpgetvalue(url);
26                 setcachedata("access_token",accesstoken,datetime.now.addhours(2));//这里缓存的是整个对象
27                 return accesstoken;
28             }
29         }
30 
31         /// <summary>
32         /// get方式请求 获取微信返回的json
33         /// </summary>
34         /// <param name="url"></param>
35         /// <returns></returns>
36         string httpgetvalue(string url)
37         {
38             //创建一个httpwebrequest
39             httpwebrequest webrequest = webrequest.createhttp(url);
40             //get 方式传输 
41             webrequest.method = "get";
42             //获取返回
43             webresponse webresponse = webrequest.getresponse();
44             //从stramreader中读取返回的流
45             string streamreader = new streamreader(webresponse.getresponsestream(), encoding.utf8).readtoend();
46             return streamreader;
47         }
48 
49 //调用
50  var accesstoken = getaccesstoken();
51                 //需要引用newtonsoft.json
52                 var wxres = jsonconvert.deserializeobject<wxresult>(accesstoken);
53                 accesstoken = wxres.access_token;
access_token

 

获取微信小程序码

细说 ASP.NET Cache 及其高级用法
 1      void htpppostvalue(string accesstoken)
 2         {
 3             var url = $"https://api.weixin.qq.com/wxa/getwxacode?access_token={accesstoken}";
 4             httpwebrequest webrequest = webrequest.createhttp(url);
 5             webrequest.method = "post";
 6             string str = "{\"path\":\" path\",\"width\":1000}";
 7             byte[] data = encoding.utf8.getbytes(str);
 8             webrequest.contentlength = data.length;
 9             stream outstream = webrequest.getrequeststream(); //写入的流
10             outstream.write(data, 0, data.length);
11             outstream.close();
12             httpwebresponse response = webrequest.getresponse() as httpwebresponse;
13             stream insstream = response.getresponsestream();
14             var imgname = datetime.now.millisecond + ".jpg";
15             var path = mappath("/content/" + imgname);
16             image img = image.fromstream(insstream);
17             img.save(path);
18         }
小程序码

 

获取微信access_token   使用httpclient

细说 ASP.NET Cache 及其高级用法
 1       string getaccesstokenwithhttpclient()
 2         {
 3             var appid = "自己appid";
 4             var appresect = "自己appresect";
 5             //实例化httpclient 对象
 6             httpclient client = new  httpclient();
 7             var url =
 8                 $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={appresect}";
 9             //使用getasybc(string url)方法异步获取httpresponsemessage  .result使用的是同步方法
10             httpresponsemessage responesemessage = client.getasync(url).result;
11             client.dispose();
12             //返回的是一个json对象
13             return responesemessage.content.readasstringasync().result;
14         }
使用httpclient获取aceess_token

 

获取微信小程序码  使用httpclient

细说 ASP.NET Cache 及其高级用法
 1  void postaccesstokenwithhttpclient(string accesstoken)
 2         {
 3             var url = $"https://api.weixin.qq.com/wxa/getwxacode?access_token={accesstoken}";
 4             string str = "{\"path\":\" path\",\"width\":1000}";
 5             //实例化httpclient 对象
 6             httpclient client = new httpclient();
 7             // 把需要传输的转换httpcontent 对象
 8             httpcontent content = new stringcontent(str,encoding.utf8);
 9             //使用postasync(string url, htppcontent content).result 获取返回的响应消息
10             httpresponsemessage response = client.postasync(url, content).result;
11             //从响应消息中获取返回的信息 使用result 同步方法
12            stream stream =  response.content.readasstreamasync().result;      
13             //二进制流转换成img
14             using (image img = image.fromstream(stream))
15             {
16                 var imgname ="123哈哈"+ datetime.now.millisecond + ".jpg";
17                 var path = mappath("/content/" + imgname);
18                 img.save(path);
19             }
20          
21         }
获取微信小程序 使用httpclient