ASP.NET Output Cache with Custom Hooks
Note
This feature is only available in NCache Enterprise edition.
Using NCache output provider, 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 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:
Pre-requisites
Include the following namespace in your application:
Alachisoft.NCache.OutputCacheProvider.dll
<!-- 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=cff5926ed6a5376"
exceptionsEnabled="false"
enableLogs="true"
cacheName="demoClusteredCache"
writeExceptionsToEventLog="false"
hookAssemblyName="OutputCache, Version=x.x.x.x, Culture=neutral, PublicKeyToken=null" hookClassName="Sample.OutputCacheProvider.OutputCacheHook.OutputCacheHookSample"/>
</providers>
</outputCache>
</caching>
Note
Replace Version=x.x.x.x with the actual NCache version that you have installed.
Sample Implementation of IOutputCacheHook
class OutputCache : IOutputCacheHook
{
public void Initialize()
{
// initialize required elements here, according to business logic.
}
public CacheMetadata OnCachingOutput(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 cacheMetaData which 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()
{
// Dispose the initialized elements here
}
}
Configuration Members
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 OutputCacheHook . |
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 input parameter. Input parameter CacheOperation type can be added or set, with respect to OutputCache 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 OutputCacheHook. |
See Also
Configuring NCache Output Caching Provider
View State Caching
ASP.NET Core