<!--main.aspx--> 
<%@ 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.aspx--> 
<%@ 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,并且獲取它的結果內容。
得到內容后,立刻輸出到客戶端。
最后把內容寫進文件里,提供給下一次做為緩存度取。
新聞熱點
疑難解答
圖片精選