In the project we often encounter some frequently accessed interfaces, and update timeliness is not particularly high, then we can use the file to do some data request cache.
Here, take the public number of fan user list For example, we put the public number of users to slow the existence of the file, so that the page will not need to re-request the interface.
Get Fan Users
/// <summary> ///User List/// </summary> /// <returns></returns> PublicActionResult userlist () {//when the page loads, the user is pulled to cacheList<wx_user_info> Users =NewList<wx_user_info>(); varOpenlist =NewWeChat (). Getopenidlist (); if(openlist.responsestate) {foreach(varIteminchOpenList.ResponseData.data.openid) {varBackdata =NewWeChat (). GetUserInfo (item); if(Backdata. ResponseState) {Users.add (backdata. ResponseData); } } varJser =NewJavaScriptSerializer (); varJSON =Jser. Serialize (Users.toarray ()); stringSrespons =JSON. ToString (); byte[] Byt =System.Text.Encoding.UTF8.GetBytes (srespons); FileStream FS=NewFileStream (Server.MapPath ("~/app_data/config/weixin.json"), FileMode.OpenOrCreate); Fs. Write (Byt,0, Byt. Length); Fs. Dispose (); Fs. Close (); } Else{Viewbag.tip="System exception, number:"+ OpenList.ErrorData.errcode +"error message:"+OpenList.ErrorData.errmsg; } returnView (); }
Asynchronously gets the specified number of bar data from the JSON file when the user pages the page
PublicActionResult Getuserlist (int? Pageint?Take ) {Page= page??1; Take= take??5; List<wx_user_info> Users =NewList<wx_user_info>(); FileStream FS=NewFileStream (Server.MapPath ("~/app_data/config/weixin.json"), FileMode.Open); byte[] Byt =New byte[FS. Length]; Fs. Read (Byt,0, Byt. Length); Users= jsonconvert.deserializeobject<list<wx_user_info>>(Encoding.UTF8.GetString (byt)); Fs. Dispose (); Fs. Close (); Viewbag.tip= Users.count () +"people"; Viewbag.totalpage= Math.ceiling ((Double) Users.count ()/(Double)) ; Viewbag.currentpage=page; varPageuseras = Users.where (UA =true); //Conditional Query if(!string. Isnullorwhitespace (request.form["Search_nickname"]) {Pageuseras= Users.where (ua = Ua.nickname.Contains (request.form["Search_nickname"]) || Ua.remark.Contains (request.form["Search_nickname"])); Viewbag.totalpage= Math.ceiling ((Double) Pageuseras.count ()/(Double)) ; Viewbag.currentpage= page =1; } //If there is no data on this page, just show the previous page if(Page >1&& Pageuseras.skip (take. Value * (page. Value-1)). Take. Value). Count () <1) {Viewbag.currentpage= Math.ceiling ((Double) Pageuseras.count ()/(Double)) ; Pageuseras= Pageuseras.skip (take. Value * (page. Value-2)). Take. Value); } Else{Pageuseras= Pageuseras.skip (take. Value * (page. Value-1)). Take. Value); } returnView (Pageuseras); }
Jsonresult Serialize and save the JSON file and read the reverse sequence to the JSON file