Alachisoft NCache 4.1 - Online Documentation

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:
 
  • Include the following namespaces in your application:
 
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:
     
  • dbsync: The value for dbSync type is either "sqlDependency" or "dbDependency". sqlDependency is a mechanism for synchronizing your cache with SQL Server 2005 database through .NET events. dbDependency allows you to synchronize your cache with any OLEDB compliant database through a configurable polling interval.
 
  • CacheKey: You can specify cache-key format here. The example above shows "Customer:CustomerID:[pk]" which would translate into "Customer:CustomerID:1000" if the CustomerID value of the object is 1000.
 
  • 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.
     
  • Enable database notifications on the database except when you are using SQL Server 2005. For further details see Enabling Database Notifications for SQL Server, Enabling Database Notifications for Oracle and Enabling Database Notifications for DB2.
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.