. Net Cache Usage

Source: Internet
Author: User

There is a weather condition in the header of the page that needs to be displayed. The iframe framework was originally used to load others' weather information for display. It was found that it was a little slow and there was a hyperlink, which was difficult to control, I found the url of the weather information returned from the Chinese weather network. The returned url is in json format, but I don't know why I cannot get it correctly using ajax, it can only be obtained by the program. Not to mention, go to the code. This is the effect.

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> // get the weather $. ajax ({url: "/class/Get_Weather.ashx", type: "POST", dataType: "JSON", error: function () {alert ("request failed ");}, success: function (data) {var weahtml = data. weatherinfo. city + "" + data. weatherinfo. date_y + "" + data. weatherinfo. week +"
Today: "+ data. weatherinfo. temp1 +" "+ data. weatherinfo. weather1 +" "+ data. weatherinfo. wind1 +" "+ data. weatherinfo. fl1 +"
Tomorrow: "+ data. weatherinfo. temp2 +" "+ data. weatherinfo. weatsp2 +" "+ data. weatherinfo. wind2 +" "+ data. weatherinfo. fl2 +"
The day after tomorrow: "+ data. weatherinfo. temp3 + "" + data. weatherinfo. weather3 + "" + data. weatherinfo. wind3 + "" + data. weatherinfo. fl3; $ (". h-Server "cmd.html (weahtml) ;}, async: true });

This is the front-end js Code. ajax is used to obtain the weather string in the general handler.



HttpContext httpContext; public void ProcessRequest (HttpContext context) {httpContext = context; // first, judge whether the cache contains the cache value where the key is weatherinfo if (httpContext. cache ["weatherinfo"] = null) // If not, call the weather Retrieval Method {GetResponseStr ();} else {// get the value context directly from the cache if any. response. contentType = "text/plain"; context. response. write (httpContext. cache ["weatherinfo"]. toString (); context. response. end ();}}////// Obtain the weather information (in json format )//////
 Private void GetResponseStr () {httpContext. response. contentType = "text/plain"; string str = "http://m.weather.com.cn/data/101180101.html"; // access the China weather network address, 101180101 is the Zhengzhou code HttpWebRequest request = (HttpWebRequest) WebRequest. create (str); HttpWebResponse response = (HttpWebResponse) request. getResponse (); Stream stream = response. getResponseStream (); // get the response data stream // convert the data stream to String string result = new StreamReader (stream, System. text. encoding. UTF8 ). readToEnd (); // Save the weather information to the cache for half an hour. The weather information does not change httpContext in a short time. cache. insert ("weatherinfo", result, null, DateTime. now. addMinutes (30), TimeSpan. zero); httpContext. response. write (result); httpContext. response. end ();}

This is the code in the general processing program.
In order not to access www.weather.com every time to obtain weather information, all the obtained values will be placed in the cache and saved for half an hour. This will increase the speed.



Welcome to the. net learning exchange group.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.