How to use a Distributed Cache for ASP.NET Output Cache?

ASP.NET Output Cache is a mechanism provided by Microsoft that allows you to keep an in memory copy of the rendered content of ASP.NET page. Due to this, ASP.NET is able to serve the subsequent user requests for this page from an in-memory cached copy instead of re-executing this page which can be quite expensive because it usually also involves database calls.

So, ASP.NET Output Cache not only improves your application performance, but also reduces a lot of expensive database trips. This improves your ASP.NET application scalability because otherwise the database would become a scalability bottleneck if all those ASP.NET pages were executed again and again.

But, the problem with the ASP.NET Output Cache is that it resides in your ASP.NET worker process address space. And, worker process resets or recycles quite frequently. When that happens, all the ASP.NET Output Cache is lost. Secondly, in case of a web garden (meaning multiple worker processes) the same page output is cached multiple times, once in each worker process and this consumes a lot of extra memory. Also, did you know that one of the most common support calls for ASP.NET is out-of-memory errors caused by ASP.NET Output Cache?

Download NCache free trial - Extremely fast and scalable in-memory distributed cacheAlso read: ASP.NET Output Cache in Microsoft Azure to Improve Performance

To overcome these limitations of ASP.NET Output Cache and to take advantage of its benefits, try using a distributed cache for storing all of ASP.NET Output Cache content. In this regard, NCache has implemented an ASP.NET Output Cache provider to enable the caching of ASP.NET rendered output in out-of-process (out-proc) cache instead of worker process address space. This way, the output of your rendered ASP.NET page is available to all other web servers in the web farm without even rendering the same ASP.NET page locally in each worker process.

By using NCache as ASP.NET Output Cache provider you can not only cache more data in out-proc cache,  but can also dramatically reduce the load on your database. This is because each rendered ASP.NET page output is accessible to all web servers in web farm without executing the page rendering process in each worker process which involves expensive database trips.

Further, NCache as ASP.NET Output Cache provider gives you the flexibility to even cache the output of certain parts of your ASP.NET page instead of complete page. This approach is very helpful in scenarios where you want certain parts of your ASP.NET to render each time. In addition, NCache also provides you high availability because even if your worker process resets or recycles, your data is not lost because it is not part of your worker process address space and resides on separate caching servers.

Steps to Configure NCache Output Caching Provider

1. Register NCache as ASP.NET Output Cache Provider: Modify your ASP.NET applications web.config to register NCache output caching provider as follows:

    <outputCache defaultProvider ="NOutputCacheProvider" />
<add name="NOutputCacheProvider"
     exceptionsEnabled="true" enableLogs="false"
     cacheName="mypartitionofReplicaCache" />

<compilation debug="true" targetFramework="4.0">
      <add assembly="Alachisoft.NCache.OutputCache,
           Version=, Culture=neutral"/>

2. Add ASP.NET Output Cache tag: Add the under mentioned Output cache tag to those pages whose output you want to cache.

<%@ OutputCache VaryByParam="ID" Duration="300">

By the way, ASP.NET versions earlier than ASP.NET 4.0 do not provide support of custom ASP.NET Output Cache providers. Therefore, to support all earlier versions of ASP.NET NCache has also implemented another version of ASP.NET Output Cache provider using an HttpModule. This HttpModule based ASP.NET Output Cache provider by NCache enables you to use distributed cache to store rendered ASP.NET page output, even if your application is using ASP.NET versions earlier than 4.0.

In summary, by using NCache output caching provider you can easily boost your ASP.NET application response time and can reduce database load.

Download NCache Trial | NCache Details

Avatar for Iqbal Khan


Leave a Reply

Your email address will not be published. Required fields are marked *