Cache with smaller granularity-"Partial View cache" and View cache
Introduction
I learned a bit about cache, including controller cache and page cache. I also saw an article about some View cache content. Here are some of my learning summaries.
Scenario
Suppose there is A page A, which is A static page, except for the carousel image of the headlines, which needs to be updated. In this case, you can cache the entire page and use Html. Partial to display the carousel image.
First, set the page cache to an hour.
The output time is displayed on the page.
The specific effect is as above. The refresh time within 60 minutes will not be changed. At this time, the entire page is cached for 60 minutes.
Partial View cache
Remove the cache filter from the Index page. Add an Html extension:
Public static class CacheExtensions {public static string Cache (this HtmlHelper htmlHelper, string cacheKey, CacheDependency cacheDependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, Func <object> func) {var cache = htmlHelper. viewContext. httpContext. cache; var content = cache. get (cacheKey) as string; if (content = null) {content = func (). toString (); // SqlCacheDependency sqldependency = new SqlCacheDependency (); // SqlCacheDependency. createOutputCacheDependency ("UserCacheDependency: UnionInf") is the configuration data var path = System that was previously written in webconfig. appDomain. currentDomain. baseDirectory; cache. insert (cacheKey, content, new CacheDependency (path + "/Exam/Name.txt"), absoluteExpiration, slidingExpiration);} return content ;}}
This is the dependency and the name.txt file under the current directory, that is, if the file is modified, the cache will become invalid. If you want to use SQL dependencies, you can use the preceding remarks.
Then, modify the page Code as follows:
The final effect is as follows:
Link: http://www.cnblogs.com/JeffreyZhao/archive/2009/09/17/aspnet-mvc-fragment-cache-1.html