NCache 4.4 - Online Documentation

Using ASP.NET Output Cache

 
 
 
This feature is not available in NCache Express and Professional edition.
 
 
Here if the same cache is being used for output caching and for other 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.GetByTag(new Alachisoft.NCache.Runtime.Caching.Tag("NC_ASP.net_output_data"));
 
Plug-in NCache Output Cache to Existing Web Applications
 
In web.config of your application, under the section of system.web element add NCache provider as a default provider i.e.,
 
For Enterprise and Professional Editions:-
<!-- 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="mypartitionofReplicaCache" exceptionsEnabled="false" enableDetailLogs="false" enableLogs="true" writeExceptionsToEventLog="false"/>"
</providers>
  </outputCache>
</caching>
 
For Open Source Edition:-
<!-- 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=1448e8d1123e9096" cacheName="mypartitionofReplicaCache" exceptionsEnabled="false" enableDetailLogs="false" enableLogs="true" writeExceptionsToEventLog="false"/>"
</providers>
  </outputCache>
</caching>
 
Replace version “x.x.x.x” with the actual NCache version that you have.
 
Configuration Members
 
 
Members
Description
cacheName
Requires string parameter. Specifies the name of the cache that have been configured through NCache Manager. The  application will use this cache for caching specified pages. If no cache name is specified, a configuration exception will be thrown.
exceptionsEnabled
Optional Boolean attribute. Specifies whether exceptions from cache API are propagated to the page output. Setting this flag is especially helpful during development phase of application since exceptions provide more information about the specific causes of failure. The default value is false.
enableLogs
Optional Boolean attribute. When this flag is set, all important events are logged including exceptions, cache initialized, disposed, and session not found etc. The default value is false.
enableDetailLogs
Optional Boolean attribute. When this flag is set, the information that is useful for debugging purposes will be included in logs. The default value is false
 
 
Using Output Cache with Custom Hooks
 
Using NCache output provide,r output of pages and controls is stored in cache.In some scenarios it can be very helpful if the page/control output can be grouped according to some x.y.z criteria before insertion into cache.Here to provide end user with further more control, NCache provides a concept of “hooks”. Hooks allow users to group output of pages/controls before its insertion into cache.
 
Following configuration changes are required:
 
<!-- 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=null" exceptionsEnabled="false" enableLogs="true" cacheName="mypartitionofReplicaCache" writeExceptionsToEventLog="false" hookAssemblyName="OutputCache, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" hookClassName="Sample.OutputCacheProvider.OutputCacheHook.OutputCacheHookSample"/>
    </providers>
  </outputCache>
</caching>
 
Replace version “x.x.x.x” with the actual NCache version that you have.
 
How to Implement IOutputCacheHook
 
Output Cache with custom hooks allows grouping data before storing into cache.To implement IOutputCacheHook, create a class which extends IOutputCacheHook. The following tables list the properties and methods that  must be implemented.
 
 
Methods
Description
 
Initialize
Initializes required elements of Output Cache hook.
 
OnCachingOutput
 
Takes as input the HttpContext instance for the current request,string key and object value generated by framework for output are passed as is as input parameter. Input parameter CacheOperation type can be added or set, with respect to Output Cache Provider operations of add and set. DateTime utcExpiry is passed as a reference parameter which allows end user code to reset expiration for particular key value pair. If the value for expiration is not set, the default value provided by framework is used.
This method returns CacheMetadata which can have additional information about group, sub group, tag, named tag and dependency settings.
This method functions as a hook between OutputCache Provider and user code, it is called when output is generated by framework but before insertion into Cache thus allowing intermediate hook for user, where additional information can be added with CacheMetadata.
 
Dispose
 
It releases resources which are no longer in use by Output Cache hook.
 
 
 
Sample OutputCacheHook
 
public class OutputCacheHookSample : IOutputCacheHook
{
 
    public void Initialize()
    {
        // initialize required elements here, according to business logic.
    }
 
    public CacheMetadata OnCachingOutput(System.Web.HttpContext context, string key, object value, CacheOperation operation, ref DateTime utcExpiry)
    {
        CacheMetadata cacheMetaData = null;
        if(context != null)
        {
              cacheMetaData = new CacheMetadata();
          // put required business logic to categorize input parameter key in //desired group. Groups, sub groups, tags, named tags and dependencies //can be assigned to object of cacheMetaDatawhich then is returned to be //inserted into cache.
            cacheMetaData.GroupName = "vendorState";
            cacheMetaData.Dependency = new KeyDependency(key);
            utcExpiry = DateTime.Now.AddMinutes(5);
 
        }
        return cacheMetaData;
    }
 
    public void Dispose()
    {
    }
        }
 
Add  Alachisoft.NCache.OutputCacheProvider.OutputCacheHook.dll in the project from %NCache installed dir% \integration\OutputCacheProvider
 
 
See Also