zoukankan      html  css  js  c++  java
  • ASP.NET Cache

    原文地址:http://www.codeproject.com/KB/aspnet/AspDotNetCache.aspx

     缓存是在内存中存储很耗时创建的数据的一项技术,也是ASP.NET一个重要特性。例如,你可以缓存那些耗时的复杂查询出来的数据,之后的请求不必再从数据库中取数,直接从缓存中取。通过缓存,可以大大提高应该程序的性能。

    主要有两种缓存类型:

    1.输出缓存

    2.数据缓存


    1. Output Caching(输出缓存)

    使用输出缓存,可以缓存页面最终的生成的HTML。当同一个页面再次请求时,缓存页起作用。ASP.NET不再执行页面的生命周期和相关代码。输出缓存的语法:

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

    Duration 属性设置页面将缓存60秒,在所有用户的请求中的第一次请求,ASP.NET执行页面代码,把生最终生成的HTML结果呈现给用户,并在缓存保存。如果服务器在60秒内再次接到同一个页面的请求,ASP.NET自动发送缓存中备份页面给用户。如果服务器在缓存过期后接到一个请求,ASP.NE执行页面代码并为下一个60秒创建一个新的HTML缓存。

     <%@ 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 (通过查询字符串缓存)

    在实际应用中,动态页面是根据参数改变页面内容的。如果你的页面是通过查询字符串来接收信息的,你也可以很容易的根据查询字符串来缓存页面的不同拷贝。VarByParam=”None” 告诉ASP.NET只存储页面的一份拷贝。VarByParam=”*”告诉ASP.NET根据不同的查询字符串存储不同的页面拷贝。

    1 <%@ OutputCache Duration="60" VaryByParam="*" %>
    2 
    3 <div align="right">
    4    <a href="OutputCachingTest2.aspx">No Query String</a> | 
    5    <a href="OutputCachingTest2.aspx?id=1">ID 1</a> | 
    6    <a href="OutputCachingTest2.aspx?id=2">ID 2</a> | 
    7    <a href="OutputCachingTest2.aspx?id=3">ID 3</a> |
    8    <a href="OutputCachingTest2.aspx?id=3&langid=1">ID 3</a>
    9 </div> 

    在同一个页面中,通过传递不同查询字符串ID值,ASP.NET针对每一个ID值存储一份拷贝,该技术在这种情况下利用的非常好,但是也存在一些问题。如果页面接收很广泛的查询字符串,那么,ASP.Net针对每一个查询字符串参数缓存一份拷贝,并且潜在的降低了重用性。这种情况下,你可以在VarByParam属性中指定几个比较重要的查询参数。

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

    如上所设置,ASP.Net根据不同的“id” 或者 “langid”查询字符串值缓存不同的版本


    Custom Caching(自定义缓存)

    你也可以创建自定义页面缓存处理过程。ASP.NET提供了一种很便捷的方式来创建自定义缓存,通过给VarByCustom属性指定自定义的缓存类型。

    1 <%@ OutputCache Duration="20" VaryByParam="None" VaryByCustom="browser" %>

    你需要创建一个方法,该方法生成一个自定义缓存字符串。语法如下:

     1 public override string GetVaryByCustomString(HttpContext context, string custom)
     2  { 
     3     if (custom == "browser")
     4     {
     5        return context.Request.Browser.Browser +
     6               context.Request.Browser.MajorVersion;
     7     }
     8     else
     9     {
    10        return base.GetVaryByCustomString(context, custom);
    11     }
    12  }

    该方法必须写在global.asax文件中,返回一个字符串值。ASP.Net用这个值来实现缓存。如果不同的请求方法返回相同的字符串值,ASP.net重用缓存页面除非产生了一个新的缓存版本。在上面例子中,GetVaryByCustomString()创建了一个基于浏览器名称的缓存字符串。ASP.Net将根据不同的浏览器请求创建不同的缓存版本。


    Control Cache(控件缓存)

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

    1  <%@ OutputCache Duration="20" VaryByControl="MyControl_1" %>

    在.aspx页面上添加上面代码,ASP.net将缓存 MyControl_1控件20秒,这样ASP.net创建一个“MyControl_1”缓存版本,如果缓存没有过期,ASP.net重用该缓存版本,除非控件代码重新执行过。如果你想创建一个依赖于控件某些属性的控件缓存,ASP.net同样很方便,只需在*.ascx控件页上添加OutPutCache指令。

    1  <%@ Control Language="C#" AutoEventWireup="true" 
    2     CodeFile="MyControl.ascx.cs" Inherits="Controls_MyControl" %>
    3  <%@ OutputCache Duration="20" VaryByControl="EmployeeID" %>
    4  ......
    5  ...... 

    VaryByControl=”EmployeeID” 告诉ASP.net根据控件属性EmployeeID不同值创建不同的控件缓存版本。

    在.ascx.cs 文件中添加属性”EmplyeeID“,用于ASP.net创建缓存

     1 private int _employeeID;
     2 public int EmployeeID
     3 {
     4    get { return _employeeID; }
     5    set { _employeeID = value; }
     6  }
     7 
     8  protected void Page_Load(object sender, EventArgs e)
     9  {
    10    lblDate.Text = DateTime.Now.ToShortDateString();
    11    lblTime.Text = DateTime.Now.ToLongTimeString();
    12 
    13    lblEmployeeID.Text = EmployeeID.ToString();
    14 
    15  }

    在页面上添加控件并设置”EmployeeID“

    1  <%@ Register Src="Controls/MyControl.ascx" TagName="MyControl" TagPrefix="uc1" %>
    2  <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    3     <div align="center">
    4         <uc1:MyControl ID="MyControl1" runat="server" EmployeeID="1"></uc1:MyControl>
    5     </div>
    6  </asp:Content>

    Cache Profile(缓存配置文件)

    ASP.net在web.config文件中定义缓存设置也是相当的方便。假如你在页面中嵌入缓存设置,并且想从20秒改为30秒,然后你必须在所有页面中更改duration值。最好的方式就是在web.config中设置缓存,你可以非常容易管理你的缓存设置。

    1 <system.web>
    2   <caching>
    3     <outputCacheSettings >
    4       <outputCacheProfiles>
    5         <add name ="ProductItemCacheProfile" duration ="60"/>
    6       </outputCacheProfiles>
    7      </outputCacheSettings>
    8    </caching>
    9  </system.web>

    现在你只需要在页面属性中添加CacheProfile=”ProfileName”就可以了。

    1  <%@ OutputCache CacheProfile ="ProductItemCacheProfile" VaryByParam="None" %>

    数据缓存(Date Cache)

    ASP.net也提供了灵活的数据缓存。你可以把一些耗资源的项添加到对象缓存集合中。Cache是一个键值对集合,添加项到缓存集合中只需要指定一个新的Key名称。

    1  Cache["Name"= data;

    该技术不支持缓存对象的控制。Cache.Insert()提供了5版本的方法重载,通过使用Insert方法,你可以设置缓存的过期策略、优先级、依赖项等。

    1  date1 = DateTime.Now;
    2  Cache.Insert("Date1", date1, null, DateTime.Now.AddSeconds(20), TimeSpan.Zero);

    Asp.net 允许设置绝对的或者相对的过期策略,但每次只能使用一个。


    Cache dependency(缓存依赖项)

    同样你也可以在ASP.net中设置缓存依赖项,缓存依赖项允许一个缓存项依赖于另一项资源,但依赖资源改变时,缓存项将自动移除。CacheDependency类用于创建依赖项。这个类有多个构造函数。你可以创建依赖于文件或者文件夹。如果文件或者文件夹改变,缓存将过期。你也可以创建依赖于其它缓存项的依赖项。

    1  date2 = DateTime.Now;
    2 
    3  string[] cacheKeys = { "Date1" };
    4  CacheDependency cacheDepn = new CacheDependency(null, cacheKeys);
    5  Cache.Insert("Date2", date2, cacheDepn);

    在上例中“Date2“缓存对项依赖于”Date1“缓存项。当”Date1“缓存对象过期时,”Date2"缓存项将自动过期。在 CacheDependency(null, cacheKeys)构造函数中,第一个参数为“null",是因为我们不想创建依赖于文件或者文件夹的缓存。我们只需要传递缓存项的Keys列表,因为我们只想创建依赖于缓存项的依赖项。


    Callback Method and Cache Priority(回调函数和缓存优先级)

    ASP.net也允许你编写一个回调函数,但缓存项从缓存中移除时,该方法将被触发。也可以设置缓存项的优先级。

      1 protected void Page_Load(object sender, EventArgs e)

     2  {
     3    DateTime? date1 = (DateTime?)Cache["Date1"];
     4    if (!date1.HasValue) // date1 == null
     5    {
     6      date1 = DateTime.Now;
     7      Cache.Insert("Date1", date1, null, DateTime.Now.AddSeconds(20), TimeSpan.Zero, 
     8                   CacheItemPriority.Default, 
     9         new CacheItemRemovedCallback(CachedItemRemoveCallBack));
    10    }
    11 
    12    DateTime? date2 = (DateTime?)Cache["Date2"];
    13    if (!date2.HasValue) // date2 == null
    14    {
    15      date2 = DateTime.Now;
    16      Cache.Insert("Date2", date2, null, DateTime.Now.AddSeconds(40), TimeSpan.Zero, 
    17                   CacheItemPriority.Default, 
    18         new CacheItemRemovedCallback(CachedItemRemoveCallBack));
    19    }
    20 
    21    // Set values in labels
    22    lblDate.Text = date1.Value.ToShortDateString();
    23    lblTime.Text = date1.Value.ToLongTimeString();
    24 
    25    lblDate1.Text = date2.Value.ToShortDateString();
    26    lblTime1.Text = date2.Value.ToLongTimeString();
    27 
    28  }
    29     
    30  private void CachedItemRemoveCallBack(string key, object value, 
    31                 CacheItemRemovedReason reason)
    32  {
    33    if (key == "Date1" || key == "Date2")
    34    { 
    35       Cache.Remove("Date1");
    36       Cache.Remove("Date2");
    37    }
    38  }

    如上例所示,我创建了”Date1"和“Date2"缓存。"Date1"20秒后过期,”Date2"40秒,但是你注意到他们同时过期。原因是因为我们注册了一个移除缓存的回调函数。当'Date1"或"Date2"过期时,将调用CachedItemRemoveCallBack方法。在这个方法中,我移除了这两个缓存项。


    Asp.net也提供了缓存项的更新回调函数。CacheItemUpdateCallback代理事件就是为这个而设计的。

  • 相关阅读:
    Jzoj4822 完美标号
    Jzoj4822 完美标号
    Jzoj4792 整除
    Jzoj4792 整除
    Educational Codeforces Round 79 A. New Year Garland
    Good Bye 2019 C. Make Good
    ?Good Bye 2019 B. Interesting Subarray
    Good Bye 2019 A. Card Game
    力扣算法题—088扰乱字符串【二叉树】
    力扣算法题—086分隔链表
  • 原文地址:https://www.cnblogs.com/zhuawang/p/2040221.html
Copyright © 2011-2022 走看看