From: http://weblogs.asp.net/scottgu/archive/2010/01/27/extensible-output-caching-with-asp-net-4-vs-2010-and-net-4-0-series.aspx
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:
[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:
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.