2013年8月14日星期三

Refresh cache OutputCache

 

Why use OutputCache

 OutputCache

I think is the most simple caching techniques, and it is aimed at the page level, simple an instruction cache can achieve the effect, effectively reduce the pressure on the server and reduce bandwidth For sites that will not frequently updated content page, we can use the OutputCache to deliver performance.

 

 

Why should update OutputCache

 

as a site manager, will certainly have to give him control of every part of the website's ability, if you want to update a Web site content, and OutputCache not fail, do you want to restart the site to take effect? At this time, an update OutputCache functionality becomes very necessary.

 

 

how to update OutputCache

 

one, webForm

 

First, we look at the effect of OutputCache in Index.aspx top of the page to add such an OutputCache directive, meaning 10 seconds for the page cache, and not directed against any parameters.

 
  
<%@ OutputCache Duration="10" VaryByParam="none"%>
 
 

then back Page_Load function, the output current of the time

 
  
 public partial class Index : System.Web.UI.Page 
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToString());
}
}
 
 

Index.aspx browser to view the page, the output of the current time, this is normal, as we continue to press F5 to refresh the current page, we will find that the output of the time did not change, even a breakpoint in the Page_Load method body not come in, this proves not to perform background function, when 10 seconds have passed, the time is updated out.

 

 

we now want to do that in the 10 seconds before the cache expires, with our approach to update the page cache.

 

Step1: Modify command to add VaryByCustom attribute

 
  
<%@ OutputCache Duration="10" VaryByParam="none"  VaryByCustom="Index_Key" %>
 
 

Step2: Create a global application file Global.asax, and the rewriting GetVaryByCustomString method

 
  
        public override string GetVaryByCustomString(HttpContext context, string custom) 
{
if (custom == "Index_Key")
{
var flag = context.Cache["Index_Key"];
if (flag == null)
{
flag
= DateTime.Now.Ticks;
context.Cache[
"Index_Key"] = flag;
}
return flag.ToString();
}
return base.GetVaryByCustomString(context, custom);
}
 
 

Step3: update OutputCache operation

 
  
        /// <summary> 
/// 更新OutputCache
/// </summary>
protected void btn_UpdateOutputCache_Click(object sender, EventArgs e)
{
HttpRuntime.Cache.Remove(
"Index_Key");
}
 
 

results as shown, refresh the page in the cache time is 42 seconds,

 

 

accordance with the above example, 10 seconds, 42 seconds cache time should all fishes, and now we've added updates OutputCache features, click, cache time is being updated, prove that we are this update OutputCache Successful! !

 

 

 

 

two, MVC

 

in MVC, there OutputCache, but unlike the WebForm as an increase in the front page directive, but rather an increase in the Controller in the Action Attribute, the following code demonstrates the Index this Action cached for 10 seconds, that is, 10 seconds Within minutes, does not perform this Action, but direct use of the cache.

 
  
public class HomeController : Controller 
{
[OutputCache(Duration
= 10)]
public ActionResult Index()
{
ViewBag.DateTime
= DateTime.Now;
return View();
}
}
 
 

View layer will simply print out

 
  
@{ 
Layout
= null;
}

<!DOCTYPE html>

<html>
<head>
<title>Index</title>
</head>
<body>
<div>
@ViewBag.DateTime
</div>
</body>
</html>
 
 

following effect: constantly refresh the page, ten seconds later, the time is updated.

 

 

MVC update WebForm OutputCache In fact, with the same idea, so I will not repeat it, end of this article I will give the source code and references in here, I would like to say my own Directives VaryByCustom properties and overridden methods GetVaryByCustomString understanding, the official explanation is jerky or not very detailed, so, or they understand better.

 

I found on MSDN VaryByCustom of two explanations:

 
  

1,

  

VaryByCustom

  

any indication custom output caching requirements of the text. browser , the cache is varied by browser name and major version information. "> If the characteristics assigned to browser , cache will vary with the browser name and major version information varies. GetVaryByCustomString method in your application's Global.asax file. "> If you enter a custom string, it must be in the application's Global . asax file rewrite GetVaryByCustomString method.

  

< span> GetVaryByCustomString method in your application's Global.asax file. "> 2,

  

< span> GetVaryByCustomString method in your application's Global.asax file. "> To set the custom string declaratively, please @ OutputCache directive includes VaryByCustom property, and the property is set for you To carry out different output cache behavior as the basis of the string.

 
 

< span> GetVaryByCustomString method in your application's Global.asax file. "> That is, if the instruction is, the use of VaryByCustom, your page cache Depending on your method will override the returned string GetVaryByCustomString changes to decide whether to update the page cache.

 

< span> GetVaryByCustomString method in your application's Global.asax file. "> us look at GetVaryByCustomString method. (Paste once, this way you can refer to the practice on MSDN, I am here is to use a custom cache to save time, remove the OutputCache action will empty the cache, cache invalidation, it will give a new time, VaryByCustom received new string, it will know to update OutputCache a)

 
  
   
  public override string GetVaryByCustomString(HttpContext context, string custom) 
        {
            if (custom == "Index_Key") //每个设置了VaryByCustom属性的页面都会进来这个方法,custom为该指令的值
            {
                var flag = context.Cache["Index_Key"];  //获取自定义缓存中的标示,这里我使用了缓存,你也可以使用别的方法
                if (flag == null)                       //假如是第一次进来,或者自定义缓存被清空了,就会走下面的方法体
                {
                    flag = DateTime.Now.Ticks;           //将当前最新时间赋予缓存
                    context.Cache["Index_Key"] = flag;
                }
                return flag.ToString();         //返回最新时间字符串,页面指令VaryByCustom接受到最新的字符串,发现跟上次的不同,就会更新OutputCache
            }
            return base.GetVaryByCustomString(context, custom);
        }
 
 

want to say here is that I rewrite the method body in time with the page output time is not related to time here purely to make a version of the iterative identification, and here I paste a given MSDN example, according to the requesting browser minor version cached.

 
  
public override string GetVaryByCustomString(HttpContext context, string arg) 
{
if(arg == "minorversion")
{
return "Version=" +
context.Request.Browser.MinorVersion.ToString();
}
return base.GetVaryByCustomString(context, arg);
}
 
 

 

reference article:

 

@ OutputCache

 

OutputCacheProfile . VaryByCustom property

 

cached user control output multiple versions

 

How to: Using Custom Strings Cache Versions page

 

 

source download, including WebForm and MVC

 

 

没有评论:

发表评论