Enabling Database Cache Dependency in NHiberate
NOTE: This feature is not available in NCache Express and Professional edition.
To enable Database Cache Dependency in NHibernate items, you need to define <ncache> configuration section in the application's configuration file (it may be app.config or web.config file) and enable database notifications on the database.
To enable DBCacheDependency in NHibernate, do the following:
using Alachisoft.NCache.Runtime.Dependencies;
using Alachisoft.NCache.Web.Caching;
-
Provide dbSync type in <ncache> section as follows:
<ncache>
<dbSync type = "sqlDependency"/>
<class name = "nhibernator.BLL.Customer">
<CacheKey format = "Customer:CustomerID:[pk]"/>
<sqlDependency sql = "select ContactName from dbo.Customers where CustomerID =?"/>
</class>
<class name = "nhibernator.BLL.Order">
<CacheKey format = " [pk]:Order"/>
<sqlDependency sql = "select ContactName from dbo.Orders where OrderID=?"/>
</class>
</ncache>
<ncache> section contains following configurable options to enable Database Cache Dependency:
-
sqlDependency: If you specify "sqlDependency" value, then you must also specify the <sqlDependency=""/> tag for individual classes in <class> section. You specify a "select" SQL statement to identify a row in SQL Server 2005 database that corresponds to this cached item. Then, whenever that row is updated or removed, this cached item is also removed. The "?" in the command will be replaced with the primary key of the mapped table at runtime.
-
See Also