您的位置:首页 > 编程语言 > ASP

asp.net 2.0 缓存技术

2009-11-27 22:54 393 查看
1. 缓存配置

缓存的配置可以通过多种方式来实现,这几种方式分别是:配置文件配置 ,单个页面配置和用户控件的配置。

(1.)配置文件配置主要是指可以在应用程序配置层次结构的任何配置文件中配置页面输出缓存设置,包括machine.config和web.config文件。

(2.)单个页面配置是指在单个页面中以声明方式或者编程方式设置缓存选项,还可以在配置文件中创建的缓存配置文件应用于单个页面。

(3.)用户控件配置是指可以在单个用户控件中以声明方式或者编程方式设置缓存,对于在其他情况下不缓存的页面内容来说,这是一种简便的的缓存方法。

▷配置文件配置:

在web.config文件里,有两个顶级配置文件用于页面输出缓存:OutputCacheSection和OutputCacheSettingsSection.前者用于配置应用程序范围的设置,例如是启用还是禁用页面输出缓存。可以向其加入enableOutputCache="false"来对整个程序禁用页面输出缓存。由于配置文件中的设置要优于单个页面中的缓存设置。后者用于配置可由单个页面使用的配置文件和依赖项,如下:

<outputCacheSettings>
<outputCacheProfiles>
<add name="CacheProfiles" duration="60">
</outputCacheProfiles>
</outputCacheSettings>


▷页面缓存配置

可以通过配置文件中定义的缓存配置文件,可以配置单个页面中的缓存。也可以在@OutputCache指令中配置单个缓存属性,或者通过设置页的类定义中的属性(attribute)进行配置。

▷用户控件配置

可以设置用户控件文件中的@OutputCache指令,或者设置控件类定义的PartialCachingAttribute属性,可以进行配置。

★asp.net页面缓存

asp.net可以缓存asp.net页所生成的部分或者全部响应,在asp.net中称为页面缓存技术。

页面缓存技术包括如何配置声明 和 如何编程设置,页面可以设置缓存的过期时间,以及检查缓存页的有效性。同时页面缓存可以缓存页面的版本以及缓存页面的不同部分。

①声明页面缓存

两种方式:一是在每个页面中直接声明,另一个就是在配置文件中定义缓存的配置属性,然后在每个页面设定该属性。

使用前者:需要在页面中包含@OutputCache指令,并定义Duration和VaryByParam属性。在@OutputCache指令中包含Location属性,并将其值定位为OutputCacheLocation枚举中的下列值之一:Any,Client,Downstream,Server,ServerAndClient或者None。

<%@ OutputCache Duration="60" VaryByParam="None"%>


使用后者:

通过配置文件声明为统一的缓存策略,然后在每个页面在分别继承。在应用程序的配置文件中web.config中定义缓存配置文件,如下:

<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="DemoTest" duration="30" varyByParam="none">
</caching>
</outputCacheSettings>
</outputCacheProfiles>


在使用配置文件的每个asp.net页中包含@OutputCache指令,并将CacheProfile属性设置为web.config文件中定义的缓存配置文件的名称,如下:

<%@ OutputCache CacheProfile="DemoTest"%>


★编程设置页面缓存

如果应用程序是在运行时进行缓存设定的,可以考虑下面的编程方式设定:

Response.Cache.SetCacheability(HttpCacheability.Public)


①设置页面缓存过期时间

在asp.net中,如果想设置页面缓存过期时间,可以通过声明的方式和编程的方式来进行设定。

前者如前面例子所示。后者如下所示:

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);


②缓存页有效性检查

protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.AddValidationCallback(new HttpCacheValidateHandler(ValidateCacheOutput), null);
}
public static void ValidateCacheOutput(HttpContext context, object data, ref HttpValidationStatus status)
{
//判断请求中是否包含status对象
if (context.Request.QueryString["Status"] != null)
{
//取出Staus对象的值
string pageStatus = context.Request.QueryString["Status"];
//判断status值的内容是否是invalid
if (pageStatus == "invalid")
{
status = HttpValidationStatus.Invalid;
}
else if (pageStatus == "ignore")
{
status = HttpValidationStatus.IgnoreThisRequest;
}
else
status = HttpValidationStatus.Valid;

}
//如果不存在,这显示不正确
else
{
status = HttpValidationStatus.Valid;
}

}


③缓存页面版本

有两种方式:一是使用@OutputCache指令属性的声明方式或者使用HttpCachePolicy类的编程方式

1.可以用来是缓存输出因查询字符串而异的缓存情况
<%@ OutputCache Duration="60" VaryByParam="Param1" %>
2.可用来是缓存输出因控制值而异的缓存情况
<%@ OutputCache Duration="60" VaryByControl="Label1;label2;lable3"%>
3.可用来因请求http表头而异的缓存情况
<%@ OutputCache Duration="60" VaryByHeader="Accept-Language" VaryByParam="None"%>
4.可以使缓存输出因浏览器类型或者定义的自定义字符串而异的缓存情况
<%@ OutputCache Duration="60" VaryByParam="None" VaryByCustom="browser" %>



缓存应用程序数据

缓存项的添加

//通过键值对添加
Cache["Test1"] = " Test1";
//使用Insert方法
Cache.Insert("Test2"," Test2");
//向缓存添加项并添加依赖项
string[] dependencies ={ " Test2" };
Cache.Insert("Test3", " Test3", new System.Web.Caching.CacheDependency(null, dependencies));
//添加有过期策略的缓存项
Cache.Insert("Test4", " Test4", null, DateTime.Now.AddMinutes(1d), System.Web.Caching.Cache.NoSlidingExpiration);
//添加有优先级的缓存项
Cache.Insert("Test5", "Test5", null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);
//通过添加Add方法添加缓存项目
string Test6 = (string)Cache.Add("Test6", " Test6", null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web
.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);



缓存项的检错

先判断在检索

string cacheString;
if (Cache["CacheItem"] = !null)
{
cacheString = (string)Cache["CacheItem"];
}
else
{
Cache.Insert("CacheItem", "Hello world");
cacheString = (string)Cache["CacheItem"];
}




缓存项的删除

缓存自动删除的原因可能是缓存已满或者已过期或者依赖项发生改变。

还可以显示移除

Cache.Remove("Test1");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: