Extensible output Caching in ASP.net 4 (you can cache pages/controls, etc.) _ Practical Tips

Source: Internet
Author: User
Tags form post
output caching past and present life
asp.net 1.0 introduces the concept of output caching, which allows developers to cache the output of pages, controls, controllers, and HTTP responses to memory. In subsequent Web requests, ASP. NET, you can use the content in the cache to respond faster.

Asp. NET's output caching system is flexible enough to allow us to cache different versions of the content based on different query strings or form post parameters. such as Test.aspx?category=vegerable and Test.aspx?category.aspx?category=meat. It also allows us to cache different versions of content based on browser type or user language preferences. For example, you can cache one copy of the application's mobile version and cache another for the desktop version.

We can also configure ASP.net to set a specific cache time (such as 1 minutes) for the cached item. We can also configure ASP.net cache entries to dynamically update the cache based on external events (such as database data updates).
However, ASP.net V1 to asp.net V3.5 only allows memory caching.

asp.net 4 output cache extension
asp.net 4 expands the output cache so that we can configure one or more output cache provider (outputs cached providers). Output cache provider can use any storage mechanism to persist output cache content. This allows us to place 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. We then weighed 4 public methods to implement Add/Remove/retrieve/Update cached content (each cache entry must be identified by a unique key). We then registered this custom output cache provider into the Web.config file as follows:

On top, I added an output cache provider named Samplecache, which is implemented by the Scottoutputcache class in the OutputCacheSample.dll assembly. I also set the asp.net default output cache provider for Samplecache, that is, through the defaultprovider above.

Now, whenever I add the following instruction to an ASPX page, the content of the page is cached through the Scottoutputcache:
<%@ OutputCache duration= varybyparam= "None"%> similar if I add the [OutputCache] attribute to an action, The content pages will be cached through the Scottoutputcache:
Copy Code code as follows:

[OutputCache (DURATION=60)]
Public ActionResult Browse (string category)
{
return View ();
}

customizing which output cache to use provider
Above I only provide a default Samplecache output cache provider. Developers can actually dynamically select output caching based on each request provider. For example, we can use the ASP.net built-in memory provider for the homepage and Top 10 pages (it's super fast because the content is in memory) and cache the infrequently requested page to disk.

We can implement the above requirements by overloading the application of the Global.asax Getoutputcacheprovidername ():
Copy Code code 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);
}
}
}

This allows us to use the ASP.net memory cache provider for home.aspx pages alone, while other requests use the cached 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.