ASP.NET Output Cache Provider Overview
NCache serves as a distributed ASP.NET Output Cache provider that removes redundant page rendering and heavy database calls in multi-server web farms. By moving processed HTML output to NCache’s in-memory storage, applications achieve linear scalability and maintain high availability even during IIS recycles. Configure NCache as your Output Cache Provider using the steps below to optimize your application’s response times.
Prerequisites
- To configure ASP.NET Output Cache Provider, install the following NuGet package in your .NET client application based on your NCache edition:
- NCache: AspNet.OutputCache.NCache
- NCache Open Source: AspNet.OutputCache.NCache.Opensource
- To use the NCache Output Cache Provider APIs, include the following namespace in your application in Startup.cs:
- The cache must be running.
- Make sure that the data being added is serializable.
- For API details, refer to: NOutputCacheProvider, GetByTag, Tag, SearchService.
Step 1: Register NCache as the Distributed Output Cache Provider
In the <system.web> element of Web.config, add NCache as the default Output Cache Provider as follows:
<!-- caching section group -->
<caching>
<outputCache defaultProvider="NOutputCacheProvider">
<providers>
<add name="NOutputCacheProvider"
type= "Alachisoft.NCache.OutputCacheProvider.NOutputCacheProvider, Alachisoft.NCache.OutputCacheProvider, Version=x.x.x.x, Culture=neutral, PublicKeyToken=cff5926ed6a53769"
cacheName="defaultCache"
exceptionsEnabled="false"
enableDetailLogs="false"
enableLogs="true"
writeExceptionsToEventLog="false"/>
</providers>
</outputCache>
</caching>
Note
Replace Version=x.x.x.x with the actual NCache version that you have installed.
Configuration Members
Following are the ASP.NET Output Cache Provider configuration members:
| Members | Description |
|---|---|
cacheName |
Specifies the name of the cache that has been configured through NCache Management Center. The application will use this cache for caching specified pages. It requires a String parameter and if no cache name is specified, a configuration exception will be thrown. |
exceptionsEnabled |
It is an optional Boolean attribute that specifies whether exceptions from cache API are propagated to the page output. Setting this flag True is especially helpful during the development phase of the application since exceptions provide more information about the specific causes of failure. The default value is False. |
enableLogs |
It is an optional Boolean attribute. When this flag is set to True, all important events are logged including exceptions, cache initialized, disposed, and session not found, etc. The default value is False. |
enableDetailLogs |
It is an optional Boolean attribute. When this flag is set to True, the information that is useful for debugging purposes will be included in the logs. The default value is False. |
Step 2: Enable Output Caching on Specific ASP.NET Pages
Add the below mentioned OutputCache tag to those pages whose output you want to cache. Its duration is specified in seconds.
<%@ OutputCache VaryByParam="ID" Duration="300">
Fetch Output Cache Data
Output cache data can be retrieved by NC_ASP.net_output_data tag. With this tag, it is easy to find data specific to Output Caching data.
Hashtable allOutputCacheData = cache.SearchService.GetByTag(new Tag("NC_ASP.net_output_data"));
See Also
Using Output Cache with Custom Hooks
View State Caching
ASP.NET Core
Alachisoft.NCache.OutputCacheProvider