现在的位置: 首页 > 综合 > 正文

ASP.NET 2.0缓存 (转)

2012年09月24日 ⁄ 综合 ⁄ 共 5581字 ⁄ 字号 评论关闭


MSDN上缓存概述:
http://msdn2.microsoft.com/zh-cn/library/726btaeh(VS.80).aspx  


一、页输出缓存

1.设置 ASP.NET 页缓存的两种方式

1.1 以声明方式设置 ASP.NET 页的缓存

以声明方式设置 ASP.NET 页的缓存的方法是在页中使用 @ OutputCache 指令,它的常用属性如下:

 

程序代码 程序代码
<%@ OutputCache Duration="" VaryByParam="" VaryByControl="" VaryByHeader="" VaryByCustom="" CacheProfile="" Location="" %>

Duration:设置缓存到期时间,单位:秒。
VaryByParam:可用来使缓存输出因查询字符串而异,多个查询字符用分号隔开。
VaryByControl:可用来使缓存输出因控制值而异。
VaryByHeader:可用来使缓存输出因请求的 HTTP 标头而异。
VaryByCustom:可用来使缓存输出因浏览器类型或您定义的自定义字符串而异。
CacheProfile:结合配置文件使用。
Location:设置页的可缓存性,值有Any,Client,Downstream,None,Server,ServerAndClient。

注:在使用 @ OutputCache 指令时,必须包括一个 VaryByParam 属性,否则将出现分析器错误。如果不希望使用 VaryByParam 属性提供的功能,请将它的值设置为“None”。

@ OutputCache 指令使用示例

①使用参数对页的各个版本进行缓存:

 

程序代码 程序代码
<%@ OutputCache Duration="60" VaryByParam="City" %>

注:如果要根据多个参数改变输出缓存,请包括以分号 (;) 作为分隔符的参数名称的列表;如果要根据所有的参数值来改变缓存,请将VaryByParam 属性设置为星号 (*);如果不要根据参数值来改变缓存,请将 VaryByParam 属性设置为"None"。

②使用 HTTP 标头对某页的各个版本进行缓存:

 

程序代码 程序代码
<%@ OutputCache Duration="60" VaryByParam="None" VaryByHeader="Accept-Language" %>

注:如果要根据多个标头改变缓存的内容,请以分号 (;) 作为分隔符包括标头名称的列表;如果要根据所有标头值改变缓存的内容,请将VaryByHeader 属性设置为星号 (*)。

③使用请求浏览器缓存页的各个版本:

 

程序代码 程序代码
<%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="browser" %>

④使用自定义字符串对页的各个版本进行缓存:

 

程序代码 程序代码
<%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="minorversion" %>

注:还要在应用程序的 Global.asax 文件中,重写 GetVaryByCustomString 方法以指定自定义字符串的输出缓存行为。参考:http://msdn2.microsoft.com/zh-cn/library/5ecf4420(VS.80).aspx

⑤结合配置文件:
将以下 XML 添加为 system.web 元素的子项:

 

程序代码 程序代码
<!-- caching section group -->
<caching>
<outputCacheSettings>
    <outputCacheProfiles>
        <add name="AppCache1" enabled="true" duration="60"/>
    </outputCacheProfiles>
</outputCacheSettings>
</caching>

@ OutputCache 指令:

 

程序代码 程序代码
<%@ OutputCache CacheProfile="AppCache1" VaryByParam="None" %>

使用这种方法我们可以从单个配置文件更改缓存行为,而无需编辑各个页面的 @ OutputCache 指令,并且还可以根据需要建立不同的缓存规则,再应用到各组单独页面中。

1.2 以编程方式设置 ASP.NET 页的缓存

以编程方式设置 ASP.NET 页的缓存的方法是在页的代码中,调用 Response 对象的 Cache 属性:

 

程序代码 程序代码
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)); //设置缓存过期时间
Response.Cache.SetCacheability(HttpCacheability.Public); //设置页的可缓存性
Response.Cache.SetValidUntilExpires(true); //缓存忽略 Cache-Control 无效标头

使用示例

①使用参数对页的各个版本进行缓存:

 

程序代码 程序代码
protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetExpires(DateTime.Now.AddMinutes(1.0));
    Response.Cache.SetCacheability(HttpCacheability.Public);
    Response.Cache.SetValidUntilExpires(true);
    Response.Cache.VaryByParams["Zip"] = true;
}

注:如果要根据多个参数改变缓存的内容,请多次设置 VaryByParams 属性。

②使用 HTTP 标头对某页的各个版本进行缓存:

 

程序代码 程序代码
protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetExpires(DateTime.Now.AddMinutes(1.0));
    Response.Cache.SetCacheability(HttpCacheability.Public);
    Response.Cache.SetValidUntilExpires(true);
    Response.Cache.VaryByHeaders["Accept-Language"] = true;
}

注:如果要根据多个标头改变缓存的内容,需要在 VaryByHeaders 属性中设置多个值。如果要根据所有标头改变缓存的内容,请将VaryByHeaders["VaryByUnspecifiedParameters"] 设置为 true。

③使用请求浏览器缓存页的各个版本:

 

程序代码 程序代码
protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetExpires(DateTime.Now.AddMinutes(1.0));
    Response.Cache.SetCacheability(HttpCacheability.Public);
    Response.Cache.SetValidUntilExpires(true);
    Response.Cache.SetVaryByCustom("browser");
}

④使用自定义字符串对页的各个版本进行缓存:

 

程序代码 程序代码
protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetExpires(DateTime.Now.AddMinutes(1.0));
    Response.Cache.SetCacheability(HttpCacheability.Public);
    Response.Cache.SetValidUntilExpires(true);
    Response.Cache.SetVaryByCustom("minorversion");
}

说明:要在应用程序的 Global.asax 文件中,重写 GetVaryByCustomString 方法以指定自定义字符串的输出缓存行为。

2.页输出缓存的几种模型

2.1 整页缓存:当设置 ASP.NET 页缓存的位置发生在页面上时,即是整页缓存。
2.2 部分页缓存(控件缓存):当设置 ASP.NET 页缓存的位置发生在用户控件上时,即是控件缓存。
2.3 部分页缓存(缓存后替换):在整个缓存的页面上以声明方式使用 Substitution 控件或以编程方式使用 Substitution 控件 API 或以隐式方式使用 AdRotator 控件,即是采用了缓存后替换。

缓存后替换举例(以声明方式使用 Substitution 控件)

aspx代码:
 

程序代码 程序代码
<asp:Label ID="Label1" runat="server" Text="Label" Width="276px"></asp:Label>
<br />
<asp:Substitution ID="Substitution1" runat="server" MethodName="NoCache" />

aspx.cs代码:
 

程序代码 程序代码
protected void Page_Load(object sender, EventArgs e)
{
    Label1.Text = DateTime.Now.ToString();
}
protected static string NoCache(HttpContext context)
{
    return DateTime.Now.ToString();
}

说明:Substitution 控件的 MethodName 属性值为一个方法的名称(本例为NoCache),对该方法的要求是它接受的参数类型必须为HttpContext且返回值类型为string,而且还必须为静态方法!

二、应用程序缓存

1.创建

方法1:Cache["CacheName"] = "CacheValue";
方法2:Cache.Insert(String key,object value,System.Web.Caching.CacheDependency dependencies,DateTime absoluteExpiration,TimeSpan slidingExpiration,System.Web.Caching.CacheItemPriority priority,System.Web.Caching.CacheItemRemovedCallback onRemoveCallback);
方法3:Cache.Add(String key,object value,System.Web.Caching.CacheDependency dependencies,DateTime absoluteExpiration,TimeSpan slidingExpiration,System.Web.Caching.CacheItemPriority priority,System.Web.Caching.CacheItemRemovedCallback onRemoveCallback);

Add方法和Insert方法的区别是Add 方法将返回您添加到缓存中的对象。另外,如果使用 Add 方法,并且缓存中已经存在与现有项同名的项,则该方法不会替换该项,并且不会引发异常。

创建示例

①通过使用 Insert 方法将项添加到缓存中:

 

程序代码 程序代码
Cache.Insert("CacheItem2", "Cached Item 2");

②通过指定依赖项向缓存添加项:

 

程序代码 程序代码
string[] dependencies = { "CacheItem2" };
Cache.Insert("CacheItem3", "Cached Item 3",new System.Web.Caching.CacheDependency(null, dependencies));

③将设有过期策略的项添加到缓存中:

 

程序代码 程序代码
Cache.Insert("CacheItem6", "Cached Item 6",null, DateTime.Now.AddMinutes(1d), System.Web.Caching.Cache.NoSlidingExpiration);

④将设有优先级设置的项添加到缓存中:

 

程序代码 程序代码
Cache.Insert("CacheItem8", "Cached Item 8",
    null, System.Web.Caching.Cache.NoAbsoluteExpiration,
    System.Web.Caching.Cache.NoSlidingExpiration,
    System.Web.Caching.CacheItemPriority.High, null);

2.检索

 

程序代码 程序代码
string cachedString;
cachedString = (string)Cache["CacheItem"];
if (cachedString == null)
{
  cachedString = "Www.Mzwu.Com";
  Cache.Insert("CacheItem", cachedString);
}

注:由于缓存中所存储的信息为易失信息,即该信息可能由 ASP.NET 移除,因此建议的开发模式是首先确定该项是否在缓存中。如果不在,则应将它重新添加到缓存中,然后检索该项。

3.移除

 

程序代码 程序代码
Cache.Remove("MyData1");

抱歉!评论已关闭.