ASP.NET缓存

介绍

缓存是在内存存储数据的一项技术,也是ASP.NET中提供的重要特性之一。例如你可以在复杂查询的时候缓存数据,这样后来的请求就不需要从数据库中取数据,而是直接从缓存中获取。通过使用缓存可以提高应用程序的性能。

主要有两种类型的缓存:

1.输出缓存Output caching
2.数据缓存Data caching

1. 输出缓存(Output Caching)

使用输出缓存,你可以缓存最后输出的HTML页面,当相同的页面再次请求的时候,ASP.NET不会再执行页面的生命周期和相关代码而是直接使用缓存的页面,语法如下:

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

Duration 属性设置页面将被缓存60妙。任何的用户请求都会被缓存,在缓冲的60秒内相同的请求都会直接使用缓存的页面。当缓存过期后ASP.NET会再次执行页面代码并且为下一个60秒创建一个新的HTML缓存。

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="OutputCachingTest.ASPx.cs" Inherits="OutputCachingTest" Title="Page" %><%@ OutputCache Duration="20" VaryByParam="None" %> <ASP:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">    <div class="title">Output Cache</div>   Date: <ASP:Label ID="lblDate" runat="server" Text="" />   Time: <ASP:Label ID="lblTime" runat="server" Text="" />        </ASP:Content>
protected void Page_Load(object sender, EventArgs e){ lblDate.Text = DateTime.Now.ToShortDateString(); lblTime.Text = DateTime.Now.ToLongTimeString();}

在这个例子中页面将被缓存20秒。

通过查询字符串缓存(Cache by Query String )

在实际应用中页面往往会根据一些参数动态的改变页面的内容。如果你的页面是通过查询字符串来获取信息的,你可以根据查询字符串很容易的缓存页面的不同拷贝。VarByParam=”None”指定ASP.NET存储缓存页面的一个拷贝。VarByParam=”*” 指定ASP.NET根据不同的查询字符串存储不同的缓存页面。

<%@ OutputCache Duration="60" VaryByParam="*" %><div align="right">   <a href="OutputCachingTest2.ASPx">No Query String</a> |    <a href="OutputCachingTest2.ASPx?id=1">ID 1</a> |    <a href="OutputCachingTest2.ASPx?id=2">ID 2</a> |    <a href="OutputCachingTest2.ASPx?id=3">ID 3</a> |   <a href="OutputCachingTest2.ASPx?id=3&langid=1">ID 3</a></div> 

上面的例子中,在查询字符串中传了不同的ID.ASP.NET为每一个ID都存储了单独的缓存页面。这种方式会有一些问题就是当查询字符串范围很广的时候。
这个时候我们可以在VarByParam 属性中指定重要的查询字符串变量的名字,如下:

<%@OutputCacheDuration="60"VaryByParam="id;langid"%> 

这样,ASP.NET可以根据id” or “langid”来缓存不同的缓存版本。

自定义缓存(Custom Caching)

你也可以创建自定义的程序来缓存页面。ASP.NET提供了一种很便捷的方式来创建自定义缓存,使用VarByCustom属性指定自定义缓存类型的名字。

你还要创建为缓存生成自定义字符串的方法,如下:

public override stringGetVaryByCustomString(HttpContext context, stringcustom){    if(custom == "browser")    {       returncontext.Request.Browser.Browser +              context.Request.Browser.MajorVersion;    }    else    {       return base.GetVaryByCustomString(context, custom);    }}  

这个方法必须写在global.asax文件中。ASP.NET使用该方法返回的字符串来实现缓存,如果这个方法在不同的请求中返回相同的字符串,ASP.NET就会使用缓存的页面,否则就会生成新的缓存版本。

上面的例子中GetVaryByCustomString()方法根据浏览器的名字创建缓存字符串,ASP.NET会根据不同的浏览器请求创建不同版本的缓存。

控件缓存(Control Cache )

上面的缓存技术可以让你很容易的缓存整个页面,如果要缓存指定控件的内容,可以通过指定VaryByControl 属性来完成。

<%@OutputCacheDuration="20"VaryByControl="MyControl_1"%>

上面代码ASP.NET将会缓存MyControl_1控件20分钟。如果要根据一些属性值来缓存控件只需要将OutPutCache指令加入*.ascx页面。

<%@Control Language="C#"AutoEventWireup="true"CodeFile="MyControl.ascx.cs"Inherits="Controls_MyControl"%><%@OutputCacheDuration="20"VaryByControl="EmployeeID"%>

VaryByControl=”EmployeeID”告诉ASP.NET根据控件中声明的EmployeeID属性来创建不同版本的缓存。

.ascx.cs 文件加入EmplyeeID属性为ASP.NET 缓存使用。

在页面中增加控件并且设置 EmployeeID.

private int_employeeID;public intEmployeeID{   get{ return_employeeID; }   set{ _employeeID = value; }}protected voidPage_Load(objectsender, EventArgs e){   lblDate.Text = DateTime.Now.ToShortDateString();   lblTime.Text = DateTime.Now.ToLongTimeString();   lblEmployeeID.Text = EmployeeID.ToString();}

缓存配置文件(Cache Profile )

web.config可以配置缓存相关的设置,

<system.web>  <caching>    <outputCacheSettings>      <outputCacheProfiles>        <addname="ProductItemCacheProfile" duration="60"/>      </outputCacheProfiles>    </outputCacheSettings>  </caching></system.web>

你可以通过设置 CacheProfile=”ProfileName” 属性 来使用上面的配置:

<%@OutputCacheCacheProfile="ProductItemCacheProfile"VaryByParam="None"%>

NET技术ASP.NET缓存,转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。