Scalable output cache in ASP. NET 4 (pages/controls can be cached)

Source: Internet
Author: User
Tags form post

Output cache past and present
ASP. NET 1.0 introduces the concept of output cache, which allows developers to cache the output of pages, controls, controllers, and HTTP responses to the memory. In subsequent Web requests, ASP. NET can use the cached content to respond faster.

The output Cache System of ASP. NET is flexible enough to cache different versions of content based on different query strings or form post parameters. For example, test. aspx? Category = Vegerable and test. aspx? Category. aspx? Category = Meat. It also allows us to cache different versions of content based on browser types or user language preferences. For example, you can cache a copy of data for the app's mobile phone version and another copy for the desktop version.

You can also configure ASP. NET to set a specific cache time (for example, 1 minute) for the cache items ). You can also configure the cache items of ASP. NET to dynamically update the cache based on external events (such as database data updates ).
However, both ASP. NET V1 and ASP. NET V3.5 allow only memory caching.

ASP. NET 4 output cache Extension
ASP. NET 4 extends the output cache so that we can configure one or more output cache providers ). The output cache provider can use any storage mechanism to persistently output the cached content. This allows us to store the cached content in a local or remote disk, database, cloud, or distributed cache engine (such as memcached or velocity ).
We can customize our output cache provider by integrating the System. Web. Caching. OutputCacheProvider class in ASP. NET. Then we need to add, remove, retrieve, and update cache content using four public methods (each cache item must be identified by a unique key ). Then we register the custom output cache provider to the web. config file, as shown below:
 
I added an output cache provider named SampleCache, which is implemented by the ScottOutputCache class in the OutputCacheSample. dll assembly. I have also set the default output cache provider of ASP. NET to SampleCache through the above defaultProvider.

Now, whenever I add the following commands to An aspx page, the page content will be cached through ScottOutputCache:
<% @ OutputCache Duration = "60" VaryByParam = "None" %> Similarly, if I add the [OutputCache] attribute to an action, the content page will be cached through ScottOutputCache:
Copy codeThe Code is as follows:
[OutputCache (Duration = 60)]
Public ActionResult Browse (string category)
{
Return View ();
}

Custom output cache Provider
Above, I only provide a default SampleCache output cache Provider. In fact, developers can dynamically select the output cache Provider based on each request. For example, we can use the ASP. NET built-in memory provider for the homepage and Top 10 pages (it is super fast because the content is in memory), and cache the infrequently used request pages to the disk.

We can achieve the above requirements by reloading the GetOutputCacheProviderName () of Global. asax of the application:
Copy codeThe Code is as follows:
Public class Global: System. Web. HttpApplication
{
Public override string GetOutputCacheProviderName (HttpContext context )\
{
If (context. Request. Path. EndsWith ("Home. aspx ")
{
Return "AspNetInternalProvider ";
}
Else
{
Return base. GetOutputCacheProviderName (context );
}
}
}

In this way, we use the ASP. NET memory cache provider for the Home. aspx page separately, while other requests use the cache provider configured in web. config.

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.