½¨Õ¾Ñ§ - ÇáËɽ¨Õ¾´Ó´Ë¿ªÊ¼£¡

½¨Õ¾Ñ§-¸öÈ˽¨Õ¾Ö¸ÄÏ,ÍøÒ³ÖÆ×÷,ÍøÕ¾Éè¼Æ,ÍøÕ¾ÖÆ×÷½Ì³Ì

ASP.NET»º´æ

ʱ¼ä:2009-11-08 21:49À´Ô´: ×÷Õß: µã»÷: ´Î
»º´æÊÇÔÚÄÚ´æ´æ´¢Êý¾ÝµÄÒ»Ïî¼¼Êõ£¬Ò²ÊÇASP.NETÖÐÌṩµÄÖØÒªÌØÐÔÖ®Ò»¡£ÀýÈçÄã¿ÉÒÔÔÚ¸´ÔÓ²éѯµÄʱºò»º´æÊý¾Ý£¬ÕâÑùºóÀ´µÄÇëÇó¾Í²»ÐèÒª´ÓÊý¾Ý¿âÖÐÈ¡Êý¾Ý£¬¶øÊÇÖ±½Ó´Ó»º´æÖлñÈ¡¡£Í¨¹ýʹÓûº´æ¿ÉÒÔÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄÜ¡£

AspDotNetCache2

 

½éÉÜ

»º´æÊÇÔÚÄÚ´æ´æ´¢Êý¾ÝµÄÒ»Ïî¼¼Êõ£¬Ò²ÊÇ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="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¸ù¾Ý²»Í¬µÄ²éѯ×Ö·û´®´æ´¢²»Í¬µÄ»º´æÒ³Ãæ¡£

<%@ 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ÊôÐÔÖ¸¶¨×Ô¶¨Ò建´æÀàÐ͵ÄÃû×Ö¡£

%@OutputCacheDuration="20"VaryByParam="None"VaryByCustom="browser"%

Ä㻹Ҫ´´½¨Îª»º´æÉú³É×Ô¶¨Òå×Ö·û´®µÄ·½·¨£¬ÈçÏ£º

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 »º´æʹÓá£

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();

}

ÔÚÒ³ÃæÖÐÔö¼Ó¿Ø¼þ²¢ÇÒÉèÖà EmployeeID.

<%@RegisterSrc="Controls/MyControl.ascx"TagName="MyControl"TagPrefix="uc1"%>
<asp:ContentID="Content1"ContentPlaceHolderID="ContentPlaceHolder1"runat="Server">
    <
divalign="center">
        <
uc1:MyControlID="MyControl1"runat="server"EmployeeID="1"></uc1:MyControl>
    </
div>
</
asp:Content>

 

 

»º´æÅäÖÃÎļþ(Cache Profile )

web.config¿ÉÒÔÅäÖûº´æÏà¹ØµÄÉèÖã¬

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

Äã¿ÉÒÔͨ¹ýÉèÖà CacheProfile=”ProfileName” ÊôÐÔ À´Ê¹ÓÃÉÏÃæµÄÅäÖãº

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

2. Êý¾Ý»º´æ(Data Caching)

ASP.NET»¹ÌṩÁËÁíÒ»ÖÖÁé»îµÄ»º´æÀàÐÍ£ºÊý¾Ý»º´æ¡£Äã¿ÉÒÔ½«Ò»Ð©ºÄ·Ñʱ¼äµÄÌõÄ¿¼ÓÈëµ½Ò»¸ö¶ÔÏ󻺴漯ºÏÖУ¬ÒÔ¼üÖµµÄ·½Ê½´æ´¢¡£

Cache["Name"] = data;

 

ÎÒÃÇ¿ÉÒÔͨ¹ýʹÓÃCache.Insert()·½·¨À´ÉèÖûº´æµÄ¹ýÆÚ£¬ÓÅÏȼ¶£¬ÒÀÀµÏîµÈ¡£

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

 

ASP.NETÔÊÐíÄãÉèÖÃÒ»¸ö¾ø¶Ô¹ýÆÚʱ¼ä»ò»¬¶¯¹ýÆÚʱ¼ä£¬µ«²»ÄÜͬʱʹÓá£

»º´æÒÀÀµÏîCache dependency

»º´æÒÀÀµÏîʹ»º´æÒÀÀµÓÚÆäËû×ÊÔ´£¬µ±ÒÀÀµÏî¸ü¸Äʱ£¬»º´æÌõÄ¿Ï×Ô¶¯´Ó»º´æÖÐÒƳý¡£»º´æÒÀÀµÏî¿ÉÒÔÊÇÓ¦ÓóÌÐòµÄ Cache ÖеÄÎļþ¡¢Ä¿Â¼»òÓëÆäËû¶ÔÏóµÄ¼ü¡£Èç¹ûÎļþ»òĿ¼¸ü¸Ä£¬»º´æ¾Í»á¹ýÆÚ¡£

date2 = DateTime.Now;

string[] cacheKeys = { "Date1"};
CacheDependency cacheDepn = newCacheDependency(null, cacheKeys);
Cache.Insert("Date2", date2, cacheDepn);

 

ÉÏÃæµÄÀý×Ó“Date2”»º´æ¶ÔÏóÒÀÀµ“Date1”»º´æÌõÄ¿£¬µ± “Date1” ¶ÔÏó¹ýÆÚºó“Date2” ½«»á×Ô¶¯¹ýÆÚ¡£CacheDependency(null, cacheKeys)ÖеĵÚÒ»¸ö²ÎÊýΪ¿ÕÊÇÓÉÓÚÎÒÃÇÖ»¼àÊÓ»º´æ¼üµÄ¸ü¸ÄÇé¿ö¡£

»Øµ÷º¯ÊýºÍ»º´æÓÅÏȼ¶(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”ÆäÖÐÒ»¸ö¹ýÆÚ¶¼»áÖ´ÐÐCachedItemRemoveCallBack ·½·¨£¬ÔÚÕâ¸ö·½·¨ÖÐÒƳýÁËÁ½¸ö»º´æÌõÄ¿£¬ASP.NET»¹ÌṩÁË´¦Àí»º´æÌõÄ¿¸üÐÂʱµÄ»Øµ÷º¯ÊýCacheItemUpdateCallback ¡£

(ÔðÈα༭£ºadmin)
Ö¯ÃζþάÂëÉú³ÉÆ÷
¶¥Ò»ÏÂ
(0)
0%
²ÈÒ»ÏÂ
(0)
0%
------·Ö¸ôÏß----------------------------
·¢±íÆÀÂÛ
Çë×Ô¾õ×ñÊØ»¥ÁªÍøÏà¹ØµÄÕþ²ß·¨¹æ£¬ÑϽû·¢²¼É«Çé¡¢±©Á¦¡¢·´¶¯µÄÑÔÂÛ¡£
ÆÀ¼Û:
񡀂:
Óû§Ãû: ÑéÖ¤Âë:µã»÷ÎÒ¸ü»»Í¼Æ¬