Alachisoft NCache 4.1 - Online Documentation

SQL Yukon Dependency

While working with databases in a multiuser environment, updates are very frequent in database. When cache is involved, you need to keep cached items synchronized with the database. One possible solution is that, on every update, database itself notifies the cache and cache expires the out of synch items. Sql Yukon Dependency is provided by NCache for this purpose. Item expires if result of the command (based on command text) changes. SQLYukonDependency is only available in NCache CLR 2.0 and requires SQL Server 2005.
Following is the step-by-step guide for using SQLCacheDependency.
Enabling Database Notification:
Enable the service broker in SQL Server 2005 or above before NCache can use SqlCacheDependency. This allows SQL Server 2005 to send event notifications to NCache to let it know that data has changed in the database.
To verify that the broker is running you can check properties in the databaseserver using SQL Management studio or run the following query:
select is_broker_enabled from sys.databases where name = '<dbname>'
Make sure that you get "1" for the database you intend to use.
Database Permissions :
If you are not a database owner (dbo) then please enable following permissions in order to use NCache SQL cache dependency. Database permissions are defined for two different modes: Default mode and Custom mode. In Default mode SQLDependency will create the SQL Service and Queue at run time. Custom mode is used by those clients who want to use SQLDependency without Service and Queue creation rights. In this case SQLDependency will not create the SQL Service and Queue at run time and clients are supposed to create it for each NCache process.
For Default Mode:
GRANT CREATE QUEUE TO <database_principal>
GRANT CREATE SERVICE TO <database_principal>
GRANT CREATE PROCEDURE TO <database_principal>
For Custom Mode:
CREATE QUEUE "NCacheSQLQueue-[IP-Address]";
CREATE SERVICE "NCacheSQLService-[IP-Address]"
ON QUEUE "NCacheSQLQueue-[IP-Address]"
GRANT RECEIVE ON "NCacheSQLQueue-[IP-Address]" TO [User Name];
GRANT RECEIVE ON QueryNotificationErrorsQueue TO [User Name] ;
GRANT SEND ON SERVICE :: "NCacheSQLService-[IP-Address]" to [User Name] ;
  • Replace [IP-Address] with node ip address and [User Name] with database user name.
  • In a clustered environment, users are supposed to create SQL Service and Queue per NCache process.
  • You can also create required SQL Service and Queue using script "NCacheServiceQueue.sql" located at "%Install Directory%/NCache/bin/resources".
Using Dependency:
  1. Create a Java project in any Java environment.
  2. Add reference of NCClient from installationFolder../NCache/java/lib/
  3. Add the following import statement in your project:
    import com.alachisoft.ncache.web.caching.*;
    import com.alachisoft.ncache.runtime.dependencies.*;
  4. Declare a variable "_cache" in the code and initialize it with "sqlDependencyCache", and clear it if this cache was used previously.
    Cache _cache = NCache.initializeCache("sqlDependencyCache");
  5. Create a String "connectionString" containing information to connect with SQL database e.g.
    String connectionString ="Data Source=Computer_Name//SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI;";
  6. Create a dependency and add it to the cache.
    CacheDependency sqlSync = new SqlCacheDependency(connectionString, "SELECT ProductID FROM dbo.Products WHERE ProductID = 006");
    _cache.insert("ProductID:006" , "my Item" , sqlSync, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal);
  7. Now update the key "6" of the Database table using the following SQL command.
    UPDATE Products SET ProductName = 'Modified Product' WHERE ProductID =6
  8. This invalidates cacheKey Dependant-ProductID6 " and is removed by NCache.
    Note : If SQL Server goes down/restarts, all dependencies become invalid and are removed from the cache .
See Also
Copyright © 2005-2012 Alachisoft. All rights reserved.