ASP.NET中的缓存使用(1)

来源:互联网 发布:java线程池状态监控 编辑:程序博客网 时间:2024/04/30 16:48


 (1)OutPutCache

首先说下几个重要的参数

 1)Duration 页或者用户控件进行缓存的时间(单位为秒)

 2)VaryByparam分号分割的字符串列表,用于使输出缓存发生变化,这些字符串与GET方法发送的查询字符串相对应,与POST发送的参数相对应,如果没有varyByParam或者VarByControl的时候页面将报错,如果不希望使用任何参数来改变指定内容请将值设置为None,否则为*

 3)VaryByCustom 表示自定义输出缓存的任意版本,如果赋予给属性browser,缓存将随浏览器版本和主要版本信息的不同而异

(1)如果页面有缓存的时间大于用户控件的缓存时间则页的输出缓存时间优先

view plaincopy to clipboardprint?
<%@ Page Language="C#" %>  
<%@ OutputCache Duration="50" VaryByParam="none" %>  
 
<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>  
 
    <form id="form1" runat="server">  
    <div>  
<%Response.Write(DateTime.Now.ToString());%>  
    </div>  
      
    <uc1:WebUserControl ID="WebUserControl1" runat="server" />  
      
    </form>  
 
<%@ Control Language="C#" ClassName="WebUserControl" %>  
<%@ OutputCache Duration="20" VaryByParam="none" %>  
<mce:script runat="server"></mce:script>  
下面是自定义控件的:  
<%Response.Write(DateTime.Now.ToString());%> 
<%@ Page Language="C#" %>
<%@ OutputCache Duration="50" VaryByParam="none" %>

<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>

    <form id="form1" runat="server">
    <div>
<%Response.Write(DateTime.Now.ToString());%>
    </div>
   
    <uc1:WebUserControl ID="WebUserControl1" runat="server" />
   
    </form>

<%@ Control Language="C#" ClassName="WebUserControl" %>
<%@ OutputCache Duration="20" VaryByParam="none" %>
<mce:script runat="server"></mce:script>
下面是自定义控件的:
<%Response.Write(DateTime.Now.ToString());%>
 

(2)上面是在页面中定义缓存在web.config中定义如下代码(然后再页面中调用也行!)

view plaincopy to clipboardprint?
<caching> 
        <outputCacheSettings> 
          <outputCacheProfiles> 
            <add name="CacheProfile" duration="30" enabled="true"/> 
          </outputCacheProfiles> 
            
        </outputCacheSettings> 
          
      </caching> 
<caching>
        <outputCacheSettings>
          <outputCacheProfiles>
            <add name="CacheProfile" duration="30" enabled="true"/>
          </outputCacheProfiles>
         
        </outputCacheSettings>
       
      </caching>

下面说的是缓存后替换(动态更新缓存页的部分)

{

1.以声明方式使用 Substitution 控件。

2.以编程方式使用 Substitution 控件 API。

3.以隐式方式使用 AdRotator 控件。

}

(2)Substitution 指定输出缓存页上已动态内容替换该控件的部分

实例代码如下

view plaincopy to clipboardprint?
<%@ Page Language="C#" %> 
<%@ OutputCache Duration="30" VaryByParam="none" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<mce:script runat="server"><!--  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        Label1.Text = DateTime.Now.ToString();  
    }  
    //当Substitution控件执行GetCurrentDateTime这个方法的时候该方法必须匹配  
    //HtttpresponseSubstitutionCallback的委托签名  
    public static string GetCurrentDateTime(HttpContext Content)  
    {  
        return DateTime.Now.ToString();  
    }  
// --></mce:script> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>无标题页</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    <p>下面是无缓存的</p> 
     
      <asp:Substitution ID="Substitution1" runat="server" MethodName="GetCurrentDateTime" /> 
     <br /> 
         <p>下面是有缓存的</p> 
     <asp:Label ID="Label1" runat="server" Text=""></asp:Label> 
          
    </div> 
      
    </form> 
</body> 
</html> 
<%@ Page Language="C#" %>
<%@ OutputCache Duration="30" VaryByParam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<mce:script runat="server"><!--
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = DateTime.Now.ToString();
    }
    //当Substitution控件执行GetCurrentDateTime这个方法的时候该方法必须匹配
    //HtttpresponseSubstitutionCallback的委托签名
    public static string GetCurrentDateTime(HttpContext Content)
    {
        return DateTime.Now.ToString();
    }
// --></mce:script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <p>下面是无缓存的</p>
  
      <asp:Substitution ID="Substitution1" runat="server" MethodName="GetCurrentDateTime" />
     <br />
         <p>下面是有缓存的</p>
     <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
       
    </div>
   
    </form>
</body>
</html>
 

(3)设置页的可缓存性

页或者自定义控件的可缓存性是指:是否在页的生命周期内在设备上缓存页,可缓存页的设备包括:发出请求的浏览器,相应请求的Web 服务器以及请求或响应流中其他任何具有缓存功能的设备,如代理服务器。

可以使用HttpCacheability


  NoCache 设置 Cache-Control: no-cache 标头。如果没有字段名,则指令应用于整个请求,且在满足请求前,共享(代理服务器)缓存必须对原始 Web 服务器强制执行成功的重新验证。如果有字段名,则指令仅应用于命名字段;响应的其余部分可能由共享缓存提供。 
Private 默认值。设置 Cache-Control: private 以指定响应只能缓存在客户端,而不能由共享(代理服务器)缓存进行缓存。 
Server 指定响应仅缓存在源服务器上。与 NoCache 选项相似。客户机接收 Cache-Control: no-cache 指令,但文档是在原始服务器上缓存的。等效于 ServerAndNoCache。 
ServerAndNoCache 指定响应仅缓存在源服务器上。与 NoCache 选项相似。客户机接收 Cache-Control: no-cache 指令,但文档是在原始服务器上缓存的。等效于 ServerAndNoCache。 
Public 设置 Cache-Control: public 以指定响应能由客户端和共享(代理)缓存进行缓存。 
ServerAndPrivate 设指示响应只能在服务器和客户端缓存。代理服务器不能缓存响应。 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ws_hgo/archive/2010/04/22/5514855.aspx