This library provides extended cache capabilities to the ASP.NET applications.

It includes the MongoDB and SQL Server output cache providers extending ASP.NET Output Cache capabilities by allowing to store cached data outside of the application process that is especially important in web-farm scenario. Below is the most common configuration via web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <configSections>
    <section name="externalCacheConfig" type="FlexiCache.ExternalCacheConfigHandler, FlexiCache" />
  </configSections>
  
  <externalCacheConfig cacheCleanupInterval="900" enableMetatags="true">
    <customOutputCache enabled="true" dbConnectionStringName="objectStore" dbName="OutputCacheDB" 
adapterType="FlexiCache.MongoDBCacheStorageAdapter, FlexiCache" /> <cacheOnDemand enabled="true" dbConnectionStringName="objectStore" dbName="CacheOnDemandDB"
adapterType="FlexiCache.MongoDBCacheStorageAdapter, FlexiCache" /> <sessionOnDemand enabled="true" dbConnectionStringName="objectStore" dbName="SessionOnDemandDB"
adapterType="FlexiCache.MongoDBCacheStorageAdapter, FlexiCache" /> </externalCacheConfig> <connectionStrings> <add name="objectStore" connectionString="mongodb://localhost/" /> <add name="cacheConnection" connectionString="data source=localhost;initial catalog=ExternalCache;user id=sa;pwd=sa" /> </connectionStrings> <system.web> <httpModules> <add name="SessionOnDemandModule" type="FlexiCache.SessionOnDemandModule, FlexiCache" /> </httpModules> <caching> <outputCache defaultProvider="AspNetInternalProvider" enableOutputCache="true"> <providers> <add name="ExternalOutputCache" type="FlexiCache.ExternalOutputCacheProvider, FlexiCache" /> </providers> </outputCache> </caching> </system.web> </configuration>

The "caching" section in web.config includes "ExternalOutputCache" provider that can be used within ASP.NET application side-by-side with MS ASP.NET internal cache provider.
In order to achieve flexibility, an additional switch is added under externalCacheConfig section to be able to turn on/off this cache provider without affecting default ASP.NET output cache and any code changes.

This library provides "Session-On-Demand" functionality - ability to separate ASP.NET Session data to subsets that can be stored outside of the main ASP.NET session and loaded on demand when it's really used rather on every request. This optimizes the out-of-proc ASP.NET session management and increases the overall site performance. These subsets of data can also be stored either in MongoDB and SQL Server. Configuration is available via externalCacheConfig/sessionOnDemand tag.

As an additional functionality, the library provides "Cache-On-Demand" that functions as an external out-of-process cache shared by all servers on web-farm. This cached data can be stored either in MongoDB and SQL Server. Configuration is available via externalCacheConfig/cacheOnDemand tag. 

An additional configuration step would be to add the following code in the application startup section (most useful in the Global.asax) to assure proper cleanup for expired data items. The cleanup interval is set to 900 sec by default. It is not recommended to set it to too small value as it will create an extended load to the system, but will not add any benefits as the code itself supports data expiration verification. The only reason for cleanup process being present is to assure the lowest possible data storage usage for the system.

//init CacheCleanupUtility 
FlexiCache.CacheCleanupUtility.CreateInstance();

Should you choose to use SQL Server as data storage for the cache, you would need to use InstallExternalCacheSqlStorage.sql script to install cache DB. The script provides instractions and represents the template to create SQL DB that would be used for cached data.

The FlexiCache.RuntimeContext class should be used as the main accessor to cache-on-demand and session-on-demand data. The ASP.NET session, as well as application variables can also be access via this class.

The FlexiCache 1.1 has ability to collect meta data (metatags) for each cached resource. This provides further functionality to remove the cached data by using application meaningful information embedded in the metatags. The Output Cache Provider is able to automatically build meta tags collection from query string and form parameters. The result metatags have the following format: "userid=12345", where UserId was a form or query string parameter that was supplied with the request. To turn metatags auto-collection on and off use the enableMetatags="true" in the config file (see above). Additionally to the auto-collected metatags you may add custom meta tag:

//set meta tag "myfavoritepage" for output cache data resulted from current response 

HttpContext.Current.Response.Cache.SetMetaTag("myfavoritepage");

The meta tags collection allows quickly find and remove one or more cached data elements when determined by the application flow. To remove the cached data use the following statement:

HttpContext.Current.Response.RemoveOutputCacheItem(new string[]{ "myfavoritepage" });

All cached items with specified meta tags will be removed from the cache. It is possible to specify more than one meta tag to limit the cached data items removal (specifing multiple tags assumes AND condition).

If you use "Session-on-Demand" functionality with MongoDB provider, it is recommended setting an additional index on your MongoDB collection. You simply run the following script against the collection:

db.CacheItem.ensureIndex({ "SessionId" : 1 },{ "name" : "_SessionId_" });

 

Last edited Feb 19, 2012 at 5:56 PM by lennygran, version 15

Comments

MattGC Oct 3, 2012 at 2:56 PM 
Thanks for giving this excellent library to the community.

Note that if you want MongoDB to be used for Page Output caching you need to change the defaultProvider from AspNetInternalProvider to ExternalOutputCache.