Cache with smaller granularity-"Partial View cache" and View cache

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.