OutPutCache Parameter Explanation
Duration : Cache time, in seconds, this property is not added unless you location=none, and is required for the rest of the time.
Location: Where the cache is placed; The value is an enumeration value:
None: Any of the remaining settings will not work when set to None
Any: The page is cached in the browser, proxy server side, and Web server side;
Client : cached in browser;
downstream : The page is cached in the browser and on any proxy server side;
None : page does not cache;
serverandclient : The page is cached on the browser and Web server side;
The default is any.
shared: used to control whether the output of a user control can be shared by multiple pages. The default value is False.
VaryByCustom : Commonly used! Any custom output-cached text.
After using this parameter, it is convenient to expire the cache. You need to override the HttpApplication.GetVaryByCustomString method in the Global.asax file:
Each method to be cached has a cached configuration: Whether it's written individually on the action: is also configured uniformly by the Web. config file: Have to write the VaryByCustom parameter: Inside is a name. This will be the system-created category word for this cache:
On this category: Multiple caches are created because of different access parameters. These caches get a string in the Getvartbycustomstring method that is overridden in Global.asax: This string corresponds to this cache: Similar to key-value pairs:
The next time you access the same category with the same parameter in the same cache: The GetVaryByCustomString () method is called first: If the resulting string is not the same as the last time: The cache is discarded. Execute the logic in the action to regenerate the cache.
Code:
[OutputCache(Duration=3600,location = System.Web.UI.OutputCacheLocation.ServerAndClient,varybycustom="goods" )] public actionresult Index () { //Response.Cache.SetOmitVaryStar ( true); return View ();}
Run Order:
First time visit to/home/index
Action read: Execution logic: Render page:
Perform the overriding method in Global.asax GetVaryByCustomString (HttpContext context, string custom) to get a value:
You then find that the VaryByCustom parameter is configured as goods:
Where the custom parameter is the VaryByCustom parameter value on the front action configuration goods
Perform a general switch structure in the GetVaryByCustomString method: Goods
The final program returns a string: No matter what the content is. The program will be recorded. With the attempt to do a correspondence:
This is the first time that the cache has ended.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The second time you visit: The program will be different from the first execution order: First execute the getvarybycustomstring custom parameter value of goods.
The final value returned will be compared to the first value.
The same thing. The program will output the cache directly. Does not execute the action method.
And if it's not the same as the first time. The cache is discarded directly. The action method is re-executed in the order in which it was first executed ... (Execute action: Render page: And then the getvarybycustomstring) rebuild
This concludes that: As long as you control each action corresponding to the VaryByCustom parameter: You can do a cache update:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
Also note a point:
OutputCache defaults to different caches depending on the parameters: So.. In the above process: Even if the parameters of the Vartbycustom are goods. But the rear ID is different. A different cache instance will also be generated
(not to be continued)
MVC Cache OutPutCache Learning Notes