生成静态页2 使用定时器定时生成静态页

模板替换法:主要适用于最终页的静态页生成,如产品展示页,新闻显示页,职位描述及个人简历显示等。它们的特点是,一经生成,基本不变的内容。我建议在添加与编辑这些内容时,就生成静态页比较合适。对于其它情况,我建议看下面内容。

ASPX页直接输出到静态页:主要适合于内容经常会变的页,由于访问量大,实时性要高,需要经常生成。这时我们直接将ASPX页输出到静态页就比较方便,编程与生成静态面互不干饶,用户访问的一直是静态页,效率也非常高。那么什么时候生成比较合适?大家都比较关心。我看有人使用这种办法,就是用户在访问到ASPX页时,加一个判断,如果静态页存在,并且时间不长(如小于5分钟)。让其转向静态页,否则生成静态页。这样做,服务器还是要做很多工作,效率不高。

     我建议,用户在访问时及网上的超连接,直接使用静态页。我们用定时程序来控制生成静态页,也就是在我们站点内启动一个时钟,让其定时执行生成静态页。效率比较高些。当然也会存在一个问题。就是在用户不访问时,定时程序仍然会运行,占用服务器时间,但是,我认为这种开销非常少。另外,我们是来解决访问量大,实时性高的大中型网站。我认为这点问题在这个目标下不能算是问题了。代码如下,供大家参考:

  1. public class ConfigHtml  
  2.   
  3. {  
  4.   
  5.     public static System.Timers.Timer htmlTimer = new System.Timers.Timer(60000);  
  6.   
  7.   
  8.   
  9.     /// <summary>  
  10.   
  11.     /// 应用程序根目录(物理根目录 如:e:/web/)  
  12.   
  13.     /// </summary>  
  14.   
  15.     public static string RootPath = null;  
  16.   
  17.     /// <summary>  
  18.   
  19.     /// 应用程序URL根目录(如:http://www.abc.com/)  
  20.   
  21.     /// </summary>  
  22.   
  23.     public static string RootUrl = null;  
  24.   
  25.   
  26.   
  27.     private static int GenCount = 0;  
  28.   
  29.   
  30.   
  31.     static ConfigHtml()  
  32.   
  33.     {  
  34.   
  35.         //初始化应用程序根目录(物理根目录 如:e:/web/)  
  36.   
  37.         RootPath = Common.GetMapPath("/") ;  
  38.   
  39.         //初始化应用程序URL根目录(如:http://www.abc.com/)  
  40.   
  41.         RootUrl = "http://" + HttpContext.Current.Request.Url.Authority + Common.AppName;  
  42.   
  43.   
  44.   
  45.         htmlTimer.AutoReset = true;  
  46.   
  47.         htmlTimer.Enabled = true;  
  48.   
  49.         htmlTimer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);  
  50.   
  51.         htmlTimer.Start();  
  52.   
  53.     }  
  54.   
  55.   
  56.   
  57.     private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)  
  58.   
  59.     {  
  60.   
  61.         //生成测试静态文件  
  62.   
  63.         string fileName = null;  
  64.   
  65.   
  66.   
  67.         //例:如果5分钟生成一次静态文件,则使用如下代码  
  68.   
  69.         if (GenCount % 5 == 0)  
  70.   
  71.         {  
  72.   
  73.             fileName = RootPath + "index.htm";  
  74.   
  75.   
  76.   
  77.             CreateStaticHtml(RootUrl + "index.aspx", fileName);  
  78.   
  79.         }  
  80.  
  81.  
  82.  
  83.         #region 设置时钟发生次数  
  84.   
  85.   
  86.   
  87.         //设置发生次数,每触发一次GenCount加1,当GenCount大于100时,归零。  
  88.   
  89.         if (GenCount > 100)  
  90.   
  91.             GenCount = 0;  
  92.   
  93.         else  
  94.   
  95.             GenCount++;  
  96.  
  97.  
  98.  
  99.         #endregion  
  100.   
  101.     }  
  102.   
  103.   
  104.   
  105.     /// <summary>  
  106.   
  107.     /// 生成静态文件方法,外部可调用  
  108.   
  109.     /// </summary>  
  110.   
  111.     /// <param name="url">动态文件的URL路径: http://www.aa.com/index.aspx</param>  
  112.   
  113.     /// <param name="toFile">html文件的物理路径:f:/web/index.htm</param>  
  114.   
  115.     public static void CreateStaticHtml(string url, string toFile)  
  116.   
  117.     {  
  118.   
  119.         StreamReader sr;  
  120.   
  121.         StreamWriter sw;  
  122.   
  123.         WebRequest HttpWebRequest1 = WebRequest.Create(url);  
  124.   
  125.         WebResponse HttpWebResponse1 = HttpWebRequest1.GetResponse();  
  126.   
  127.         sr = new StreamReader(HttpWebResponse1.GetResponseStream(), System.Text.Encoding.UTF8);  
  128.   
  129.         string strHtml = sr.ReadToEnd();  
  130.   
  131.         sw = File.CreateText(toFile);  
  132.   
  133.         sw.WriteLine(strHtml);  
  134.   
  135.         sw.Close();  
  136.   
  137.     }  
  138.   
  139. }  
  1. 注:Common为我定义的常用方法类。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值