Microsoft ASP.NET Output Cache provides functionality to cache rendered content of ASP.NET pages or user controls for a specified duration. This allows your ASP.NET application to serve all subsequent requests from the cache instead of re-rendering and re-execution of a page.
You add the <% @OutputCache %> directive on the page to use ASP.NET Output Cache.
<%@Page … %>
ASP.NET Output Caching is a very useful feature, especially for situations when a page is accessed more frequently than it changes and you serve it from cache. This improves application performance by avoiding page re-executions and also by reducing your expensive database trips especially when page involves a lot of heavy database operations. This also improves application scalability because the database generally becomes a scalability bottleneck when there are millions of such pages and requests involving database operations.
Problems with ASP.NET Output Cache in Microsoft Azure Environment
When you use Output Cache in Microsoft Azure then page output is stored as InProc within your Microsoft Azure Web Role by default. First problem with this is that it limits you to the memory that is available on your Web Role instance and this may create an out of memory issue when you cache a large amount of page output data. Another issue is that your application runs on multiple load balanced Microsoft Azure Web Role instances. The next request might go to another Web Role instance, which creates a new copy of ASP.NET Output Cache data in this instance, as well. These redundant copies of page outputs in each Web Role instance consume a lot of extra memory.
Microsoft Azure Web Role instances also recycle quite frequently for maintenance and patching. When this happens, all page outputs are lost and you’ll have to re-execute all pages to re-populate your page Output Cache, which is a negative performance impact on your Azure application.
How to Resolve Output Caching Problems in Microsoft Azure?
One way you can resolve all these issues in Microsoft Azure is to use a distributed cache, which runs out-of-process and is a common store for all Microsoft Azure Web Role instances. ASP.NET 4.0 has introduced an extensibility point that helps developers to use any distributed cache of their choice as their ASP.NET Output Cache store.
Distributed cache is shared by all Microsoft Azure Web Roles for page outputs so there are no redundant copies made within individual web role instances. Microsoft Azure Web Roles become purely stateless so data is never lost when Web Roles are recycled. You can cache a huge amount of data in distributed cache by pooling memory resources of all cache servers together. Moreover, distributed cache reduces load on your database because you don’t have to go through page executions involving database calls in each Microsoft Azure Web Role instance separately.
NCache for Azure is an in-memory distributed cache for .NET applications deployed in Microsoft Azure cloud. NCache for Azure has implemented ASP.NET Output Cache provider which you can use to store ASP.NET page output and resolve all above mentioned issues. Additionally, deploying NCache as a Azure caching service provides data reliability with replication as well as improves application scalability.
How to use NCache for Azure ASP.NET Output Cache Provider
You can use NCache for Azure for output caching in ASP.NET as it follows without any code change to your Microsoft Azure application.
Step 1: Add Reference of NCache for Azure Output Cache provider assembly.
<compilation debug="true " targetframework="4.0">
Step 2: Register NCache for Azure Output Cache Provider under <configuration> section and provide cache settings.
<add name="NOutputCacheProvider" type="NCOutputCache.NOutputCacheProvider" exceptionsenabled="true" enablelogs="false" cachename="mypartitionofReplicaCache">
Step 3: Add ASP.NET Output Cache directive on the page that you want to cache.
Documentation: Using ASP.NET Output Caching with NCache
NCache for Azure ASP.NET Output Cache Features
NCache for Azure provides a rich set of features for caching and managing Output Caching of Microsoft Azure. Below is a list of them:
- Specify duration for Page Output: NCache for Azure allows you to specify a duration for which you want to cache ASP.NET page output.
- Cache different versions of a page: NCache for Azure allows you to cache different versions of page depending on the various ASP.NET Output Cache directives such as VaryByParam,VaryByCustom, VaryByControl. Another version of page output is stored in distributed cache if a different param is received for a page request.
- Cache different portions of a page: You can also specify only portions of the page instead of caching entire page. This is for situations where you cache only static portion of the page and leave the dynamic part which is rendered at runtime.
- Implement Custom Hooks for ASP.NET Output Cache: NCache for Azure allows you to implement and register your custom hooks (interface) for page output. This is to attach some extended attributes to your page outputs such as NCache for Azure database dependencies, Tags, Groups, etc.
As you have seen, distributed cache allows you to cache ASP.NET page outputs, which resolves your application issues with multiple load balanced Azure Web Roles. NCache is an Azure ASP.NET Output Cache provider which helps improve ASP.NET application performance scalability and reliability.