//尝试读取已有文件 Stream s = GetFileStream (filename); if (s != null)//如果文件存在并且读取成功 { using (s) { Stream2Stream (s, Response.OutputStream); Response.End (); } }
//调用Main_Execute,并且获取其输出 StringWriter sw = new StringWriter (); Server.Execute ("Main_Execute.aspx", sw);
<%@ page language="C#" %>
<%@ import namespace=System.IO %>
<script runat="server">
protected override void OnInit (EventArgs e)
{
int id;
try
{
id = int.Parse (Request.QueryString["id"]);
}
catch
{
throw (new Exception ("页面没有指定id"));
}
string filename=Server.MapPath("statichtml_"+id+".html");
//尝试读取已有文件
Stream s = GetFileStream (filename);
if (s != null)//如果文件存在并且读取成功
{
using (s)
{
Stream2Stream (s, Response.OutputStream);
Response.End ();
}
}
//调用Main_Execute,并且获取其输出
StringWriter sw = new StringWriter ();
Server.Execute ("Main_Execute.aspx", sw);
string content = sw.ToString ();
//输出到客户端
Response.Write(content);
Response.Flush();
//写进文件
try
{
using (FileStream fs = new FileStream (filename, FileMode.Create, FileAccess.Write, FileShare.Write))
{
using (StreamWriter streamwriter = new StreamWriter (fs, Response.ContentEncoding))
{
streamwriter.Write (content);
}
}
}
finally
{
//Response.End ();
}
}
static public void Stream2Stream (Stream src, Stream dst)
{
byte[] buf = new byte[4096];
while (true)
{
int c = src.Read (buf, 0, buf.Length);
if(c==0)
return;
dst.Write (buf, 0, c);
}
}
public Stream GetFileStream(string filename)
{
try
{
DateTime dt = File.GetLastWriteTime (filename);
TimeSpan ts=dt - DateTime.Now;
if(ts.TotalHours>1)
return null; //1小时后过期
return new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read);
}
catch
{
return null;
}
}
</script>
<!--Main_Execute.aspx-->
<%@ page language="C#" %>
<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>ID:
<%=Request.QueryString["id"]%></body>
</html>其中原理是这样的.
Main_Execute.aspx是生成HTML的页面.现在用Main.aspx来对它进行缓存.
过程如下:首先根据页面参数算出文件名.(这个例子只根据Request.QueryString["id"]来算)
尝试读取缓存的文件.如果成功,那么Response.End();
如果不成功:
使用Server.Execute来调用Main_Execute.aspx,并且获取它的结果内容.
得到内容后,立刻输出到客户端.
最后把内容写进文件里,提供给下一次做为缓存度取.
public static bool WriteFile(string strText,string strContent,string strAuthor)
{
string path = HttpContext.Current.Server.MapPath("/news/");
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("/news/text.html");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str =str.Replace("ShowArticle",strText); //模板页中的ShowArticle
str = str.Replace("biaoti",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// 写文件
try
{
sw = new StreamWriter(path + htmlfilename , false, code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;此函数放在Conn.CS基类中了
在添加新闻的代码中引用 注:工程名为Hover
if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出错!");
}
-------------------------------------------------------------------------
模板页Text.html代码
-------------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ShowArticle</title>
<body> biaoti
<br>
content<br>
author
</body>
</HTML>
---------------------
什么叫Cache系统,它的作用是什么?to cppTrier(cnblogs.com/hush)
---------------------
什么叫URL Rewriting技术?
应该是利用ISAPI实现URL 重写吧,这个其实换汤不换药
其实程序还是动态执行
不是什么url rewriting或者写cache的
这些你用过批量下载就知道了
Stream会耗费大量的内存
rewrite会反复调用同一个页面,这些对于大型的系统来说都不是很好的解决方案
带宽,cpu 时间,内存等等都是有限的资源,而硬盘反而是最容易获得的
用IO的file.write很容易就能在站点下创建一个文件了
这个是Cache系统要做的事,如果你通过程序来实现,本身的开销已经很大了,这是一种拆了东墙补西墙的做法,你应该配一套Cache系统,让它来完成这个事。
对于.Net而主,html,or aspx没有什么区别。
归根结底还是Cache管理的问题,你可以把页面cache到web服务器的内存,或以文件形式存在硬盘上(这也算是cache吧),对于某个页面或文件的生存期管理(从创建、索引、删除)应该考虑如何细化Cache的管理来提高性能。
另:sina 还有浙江政府网 http://www.zhejiang.gov.cn 搞不清楚他后面是用什么写的(.Net ??) 。
大家补充发言。
呵呵~~也是利用.net来生成 html文件 的。。~~~~~~~~~~~~~~`