介绍缓存是在内存存储数据的一项技术,也是ASP.NET中提供的重要特性之一。例如你可以在复杂查询的时候缓存数据,这样后来的请求就不需要从数据库中取数据,而是直接从缓存中获取。通过使用缓存可以提高应用程序的性能。 主要有两种类型的缓存: 1.输出缓存Output caching
1. 输出缓存(Output Caching)使用输出缓存,你可以缓存最后输出的HTML页面,当相同的页面再次请求的时候,ASP.NET不会再执行页面的生命周期和相关代码而是直接使用缓存的页面,语法如下: <%@ OutputCache Duration=”60” VaryByParam=”None” %>
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="OutputCachingTest.aspx.cs" Inherits="OutputCachingTest" Title="Untitled 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 )在实际应用中页面往往会根据一些参数动态的改变页面的内容。如果你的页面是通过查询字符串来获取信息的,你可以根据查询字符串很容易的缓存页面的不同拷贝。 <%@ 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>
<%@OutputCacheDuration="60"VaryByParam="id;langid"%>
这样,ASP.NET可以根据id” or “langid”来缓存不同的缓存版本。
自定义缓存(Custom Caching) 你也可以创建自定义的程序来缓存页面。ASP.NET提供了一种很便捷的方式来创建自定义缓存,使用 %@OutputCacheDuration="20"VaryByParam="None"VaryByCustom="browser"% 你还要创建为缓存生成自定义字符串的方法,如下: public override stringGetVaryByCustomString(HttpContext context, stringcustom)
这个方法必须写在 上面的例子中 控件缓存(Control Cache )上面的缓存技术可以让你很容易的缓存整个页面,如果要缓存指定控件的内容,可以通过指定 <%@OutputCacheDuration="20"VaryByControl="MyControl_1"%>
上面代码ASP.NET将会缓存 <%@Control Language="C#"AutoEventWireup="true"CodeFile="MyControl.ascx.cs"Inherits="Controls_MyControl"%>
在 private int_employeeID; 在页面中增加控件并且设置 <%@RegisterSrc="Controls/MyControl.ascx"TagName="MyControl"TagPrefix="uc1"%>
缓存配置文件(Cache Profile )
<system.web> 你可以通过设置 %@OutputCacheCacheProfile="ProductItemCacheProfile"VaryByParam="None"% 2. 数据缓存(Data Caching)ASP.NET还提供了另一种灵活的缓存类型:数据缓存。你可以将一些耗费时间的条目加入到一个对象缓存集合中,以键值的方式存储。 Cache["Name"] = data;
我们可以通过使用 date1 = DateTime.Now; Cache.Insert("Date1", date1, null, DateTime.Now.AddSeconds(20), TimeSpan.Zero);
ASP.NET允许你设置一个绝对过期时间或滑动过期时间,但不能同时使用。 缓存依赖项Cache dependency缓存依赖项使缓存依赖于其他资源,当依赖项更改时,缓存条目项将自动从缓存中移除。缓存依赖项可以是应用程序的 Cache 中的文件、目录或与其他对象的键。如果文件或目录更改,缓存就会过期。 date2 = DateTime.Now;
上面的例子“Date2”缓存对象依赖“Date1”缓存条目,当 “Date1” 对象过期后“Date2” 将会自动过期。 回调函数和缓存优先级(Callback Method and Cache Priority)ASP.NET允许我们写一个回调函数,当缓存条目从缓存中移除的时候触发。还可以设置缓存条目的优先级。 protected void Page_Load(object sender, EventArgs e) { DateTime? date1 = (DateTime?)Cache["Date1"]; if (!date1.HasValue) // date1 == null { date1 = DateTime.Now; Cache.Insert("Date1", date1, null, DateTime.Now.AddSeconds(20), TimeSpan.Zero, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack)); } DateTime? date2 = (DateTime?)Cache["Date2"]; if (!date2.HasValue) // date2 == null { date2 = DateTime.Now; Cache.Insert("Date2", date2, null, DateTime.Now.AddSeconds(40), TimeSpan.Zero, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack)); } // Set values in labels lblDate.Text = date1.Value.ToShortDateString(); lblTime.Text = date1.Value.ToLongTimeString(); lblDate1.Text = date2.Value.ToShortDateString(); lblTime1.Text = date2.Value.ToLongTimeString(); } private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason) { if (key == "Date1" || key == "Date2") { Cache.Remove("Date1"); Cache.Remove("Date2"); } }
例子中创建了“Date1” 和 “Date2”缓存。“Date1” 在20秒后过期“Date2”为40秒。但是由于我们注册了移除的回调函数,当“Date1” 或 “Date2”其中一个过期都会执行 |