缓存分为两种,一般都是页面缓存以及应用程序缓存。
页面缓存
可以分别为每个页配置页缓存,也可以在 Web.config 文件中创建缓存配置文件。利用缓存配置文件,只定义一次缓存设置就可以在多个页中使用这些设置。
首先看一个常用的,用来控制页面或者用户控制输出的持续输出30s时间,WebForm只需要在aspx页面写个OutPutCache:
<%@ OutputCache Duration="30" VaryByParam="None" %>
asp.net mvc页面生命周期跟WebForm那一套不一样,这个时候就需要在Action动手改改:
[OutputCache(Duration = 30,VaryByParam="none")] public ActionResult CacheTest() { return View(); }
页面缓存的实现和处理方式有很多种,先从OutPutCache中的各种特性的,每种特性,对应着不同的实现方式:
<%@ OutputCache Duration="100"(必填) Location="Any | Client | Downstream | Server | None | ServerAndClient " Shared="True | False" VaryByControl="控件名称" VaryByCustom="Test" VaryByHeader="headers" VaryByParam="parametername" VaryByContentEncoding="encodings" CacheProfile="cache profile name | ''" NoStore="true | false" SqlDependency="database/table name pair | CommandNotification" ProviderName="Provider Name" %>
OutPutCache上面的设置很简单,Duration是必填的,页或用户控件进行缓存的时间(秒),Location是指定缓存的位置,由枚举OutputCacheLocation来确定,包含在用户控件(.ascx 文件)中的 @ OutputCache 指令不支持此特性。
有的时候我们缓存整个页面肯定不是太现实的,很多情况下只是缓存一部分页面,新建一个用户控件:
1 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Test.ascx.cs" Inherits="MyWebForm.Test" %> 2 <%@ OutputCache Duration="5" VaryByParam="None" %> 3 <% string t = DateTime.Now.ToString(); 4 Response.Write(t); %>
在aspx页面中使用用户控件:
1 <%@ Register Src="~/Test.ascx" TagPrefix="uc1" TagName="Test" %> 2 <%@ OutputCache Duration="10" VaryByParam="None" %> 3 <uc1:Test runat="server" ID="Test1" />
这里面涉及到一个问题,就是页面设置的时间和控件设置的时间,页面是10s,控件是5s,控件与页面都是10s刷一次,以页面时间为主;如果页面是10s,控件是20s,那么页面10s刷一次,控件20s刷一次,简单点理解就是控件以时间长的为主;
asp.net mvc 中ascx所有的功能就是View页面,跟局部视图调用一样,新建一个Test.ascx,通过控制器去访问:
1 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> 2 <% string t = DateTime.Now.ToString(); Response.Write(t); %>
1 [OutputCache(Duration=5,VaryByParam="none")] 2 public ActionResult Test() 3 { 4 Response.Cache.SetOmitVaryStar(true); 5 return View(); 6 }
注意:控制器多加了Response.Cache.SetOmitVaryStar(true),这个Bug还比较隐蔽,详情可参考:
http://www.cnblogs.com/dudu/archive/2012/08/27/asp_net_mvc_outputcache.html
如果输入自定义字符串,则必须在应用程序的 Global.asax 文件中重写 GetVaryByCustomString方法, 根据用户的主机名或者浏览器起版本给出缓存,不存在的话就需要新建缓存,如果存在话就不需要新建缓存。
public override string GetVaryByCustomString(HttpContext context, string arg) { if (arg == "Test") { return "浏览器版本=" + context.Request.Browser.MinorVersion.ToString(); //return "用户主机=" + // context.Request.UserHostName; } return base.GetVaryByCustomString(context, arg); }
VaryByHeader
@ OutputCache指令不支持此特性。
VaryByParam
POST 参数名称。
页面通过URL链接访问,要缓存页面,百分之九十的情况的下页面是会有参数传递的,如果你设置为none,那么你用get或者post请求的时候是不会缓存的,设为*是最简单的什么都不用管,如果你只想查name的时候缓存一下,那么你直接设置一下就行.
[OutputCache(Duration = 30,VaryByParam="name")] public ActionResult CacheTest(string name) { if (name=="1") { ViewData["Test"] = "测试"; } return View(); }
CacheProfile
如果此名称与配置文件项不匹配,将引发异常。
[OutputCache(CacheProfile = "myProfile",VaryByParam="none")] public ActionResult Test() { Response.Cache.SetOmitVaryStar(true); return View(); }
<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="myProfile" duration="10"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
</system.web>
Shared@ OutputCache 指令不支持此特性。
NoStore一个布尔值,它决定了是否阻止敏感信息的二级存储。在用户控件(ascx)中使用的时候需要注意加上Response.Cache.SetNoStore();
SqlDependency标识一组数据库/表名称对的字符串值,页或控件的输出缓存依赖于这些名称对。
ProviderName 一个字符串值,标识要使用的自定义输出缓存提供程序。
VaryByContentEncodings 以分号分隔的字符串列表,用于更改输出缓存。
应用程序缓存
这一模式可确保缓存中始终有最新的数据。
通过键和值直接设置项向缓存赋值
List<Person> list = new List<Person>(){new Person(){Name="Small",Age=24}, new Person(){Name="Fly",Age=24}, new Person(){Name="Elephant",Age=24}}; Cache["Test"] = list;
Insert的方式
Cache.Insert("Fly", "缓存2");
添加依赖项,可以是其他已存在的缓存,也可以是已存在的文件:
Cache.Insert("Fly", "缓存2"); Cache.Insert("Elephant", "缓存3",new CacheDependency(null, new string[]{"Fly"}));//key为Elephant, value为缓存3,依赖于key为Fly的缓存
Cache.Insert("Elephant", "缓存3",new CacheDependency(Server.MapPath("路径")));
插入多个缓存依赖
CacheDependency dep1 = new CacheDependency(null, new string[] { "Fly" }); CacheDependency dep2 = new CacheDependency(Server.MapPath("路径")); AggregateCacheDependency cacheList = new AggregateCacheDependency(); cacheList.Add(dep1); cacheList.Add(dep2); Cache.Insert("Elephant", "缓存3",cacheList);
绝对过期和滑动过期这个两个名字比较有意,绝对过期枚举英文是NoSlidingExpiration(不滑动),滑动过期NoAbsoluteExpiration(不绝对),这里面还设置了CacheItemPriority,这枚举大家自己看下就行,还有一个自动删除,缓存到期后执行删除方法:
//绝对过期时间的时间为30s,过完30s之后就没有缓存 Cache.Insert("Tiger", "老虎",null, DateTime.Now.AddSeconds(30), Cache.NoSlidingExpiration,CacheItemPriority.High,Show); //滑动过期时间的时间为1分钟,只要一直刷新一直有,如果刷新间隔超过一分钟的缓存就没啦 Cache.Insert("Dragon", "龙", null, Cache.NoAbsoluteExpiration,new TimeSpan(0, 0, 60)); public void Show(string key, object value, CacheItemRemovedReason call) { if (key=="Pig") { Cache.Remove("Pig"); } }
Add的形式(该方法没有重载)
Cache.Add("Cow", "牛", null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
可扩展性缓存
也可以对所呈现页面的各种变化因素组合进行缓存,但应使用分布式缓存以减少前端 Web 服务器的内存消耗。实现可扩展,需要创建一个继承自 OutputCacheProvider 的类:
public class MyCacheProvide : OutputCacheProvider { public override object Add(string key, object entry, DateTime utcExpiry) { throw new NotImplementedException(); } public override object Get(string key) { throw new NotImplementedException(); } public override void Remove(string key) { throw new NotImplementedException(); } public override void Set(string key, object entry, DateTime utcExpiry) { throw new NotImplementedException(); } }