aspx页面的缓存功能自己定做
----------------------------------
.net里 aspx有缓存功能,在页面加入OutPutCache指示即可。 但是有个小缺点就是
时间固定了就不能改变了。(有谁知道请告诉我) 比如设置过期时间为10分钟。 那么
在这10分钟里,比如数据库里商品价格变了。 想立刻刷新这个页面的缓存。 难道要等到10分钟后才更新吗。 以下的例子可以实现。
----------------------------------
<%@ Page Language="C#" Debug="true" AutoEventWireup="true" %>
<%@Import Namespace="System" %>
<%@Import Namespace="System.Text" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.SqlClient" %>
<%@Import Namespace="System.Configuration" %>
<%@Import Namespace="System.Collections" %>
<%@Import Namespace="System.Web" %>
<%@Import Namespace="System.Web.Security" %>
<%@Import Namespace="System.Web.UI" %>
<%@Import Namespace="System.Web.UI.WebControls" %>
<%@Import Namespace="System.Web.UI.WebControls.WebParts" %>
<%@Import Namespace="System.Web.UI.HtmlControls" %>
<%@Import Namespace="System.Data.SqlClient" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
<meta http-equiv='Content-Type' content='text/html; charset=SJIS' />
</head>
<body>
<form id="form1" method ="post" >
<div id="headbox3">
<% group_num = 2; %>
<%for(int i=0;i<group_num;i++){ %>
bbb<br />
<% } %>
<% = this.title %>
<% = DateTime.Now.ToLongTimeString() %>
</div>
</form>
</body>
</html>
<script language="C#" runat="server" >
private string title = "kkkkkkkkkk";
private int group_num = 0;
//abcdefg
protected void Page_Load(object sender, EventArgs e)
{
//System.Diagnostics.Debugger.Break();
string strHtm = (string)System.Web.HttpContext.Current.Cache["strHtm"];
bool goodsPriceChanged;
goodsPriceChanged = false;
if (strHtm != null && strHtm.Length > 1 && goodsPriceChanged==false)
{
Response.Write(strHtm);
Response.End();
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
//System.Diagnostics.Debugger.Break();
System.IO.TextWriter tw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(tw);
Page.RenderControl(htw);
string strHtm = htw.InnerWriter.ToString();
//Cache.Insert("strHtm",strHtm ,null,DateTime.MaxValue,TimeSpan.FromSeconds(10));
Cache.Insert("strHtm", strHtm, null, DateTime.Now.AddSeconds(5), Cache.NoSlidingExpiration);
}
</script>