Generate static page 3 Use timer to generate static page

Source: Internet
Author: User

Previous two articlesArticleThis article discusses how to generate static pages and describes two common methods: Template replacement and ASPX page output to static files.

The following question is: when to generate a static page?In my personal experience, I think:

Template replacement: It is mainly used to generate static pages on the final page, such as the product display page, news display page, job description, and resume display. These features are basically unchanged once generated. It is recommended that you generate a static page when adding and editing the content. For other cases, I suggest you read the following content.

The ASPX page is output directly to the static page.: It is mainly suitable for pages with frequently changed content. Because of the large access volume and high real-time performance, it is often generated. In this case, it is convenient to directly output the ASPX page to a static page. Programming and generating static pages do not work with each other. Users have been accessing static pages, and the efficiency is also very high. When is the generation more appropriate? Everyone is concerned about it. I think some people use this method, that is, when the user accesses the ASPX page, add a judgment. If the static page exists and the time is not long (for example, less than 5 minutes ). Turn it to a static page. Otherwise, a static page is generated. In this way, the server still needs to do a lot of work and the efficiency is not high.

I recommend that you directly use static pages when accessing the service and using the hyperconnections on the Internet. We use timingProgramTo control the generation of static pages, that is, to start a clock in our site and let it regularly execute to generate static pages. High efficiency. Of course there will also be a problem. That is, when the user does not access the system, the timer program will still run, occupying the server time. However, I think this overhead is very small. In addition, we aim to solve large and medium-sized websites with high access volume and real-time performance. I think this problem cannot be considered a problem under this goal.CodeFor your reference:

Public class confightml {public static system. timers. timer htmltimer = new system. timers. timer (60000); // <summary> // application root directory (physical root directory such as: e:/web /) /// </Summary> Public static string rootpath = NULL; // <summary> // application URL root directory (e.g., http://www.abc.com /) /// </Summary> Public static string rooturl = NULL; Private Static int gencount = 0; static confightml () {// initialize the application root directory (for example, the physical root directory: e:/web/) rootpath = C Ommon. getmappath ("/"); // initialize the application URL root directory (e.g.: http://www.abc.com/) rooturl = "http: //" + httpcontext. current. request. URL. authority + common. appname; htmltimer. autoreset = true; htmltimer. enabled = true; htmltimer. elapsed + = new system. timers. elapsedeventhandler (timer_elapsed); htmltimer. start ();} Private Static void timer_elapsed (Object sender, system. timers. elapsedeventargs e) {// generate a test static file string fi Lename = NULL; // example: if a static file is generated every 5 minutes, use the following code if (gencount % 5 = 0) {filename = rootpath + "index.htm "; createstatichtml (rooturl + "index. aspx ", filename) ;}# region sets the number of clock occurrences // sets the number of occurrences. gencount increases by 1 for each trigger, and returns to zero when gencount is greater than 100. If (gencount> 100) gencount = 0; else gencount ++; # endregion} // <summary> // method for generating static files, externally callable /// </Summary> /// <Param name = "url"> URL path of the dynamic file: http://www.aa.com/index.aspx </param> /// <Param name = "tofile"> physical path to the HTML file: F:/web/index.htm </param> Public static void createstatichtml (string URL, string tofile) {streamreader SR; streamwriter SW; webrequest httpwebrequest1 = webrequest. create (URL); webresponse httpwebresponse1 = httpwebrequest1.getresponse (); Sr = new streamreader (httpwebresponse1.getresponsestream (), system. text. encoding. utf8); string strhtml = sr. readtoend (); Sw = file. createtext (tofile); Sw. writeline (strhtml); Sw. close ();}}
Note: common is a common method class defined by me.

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.