Alachisoft NCache 4.1 - Online Documentation

Enabling Notifications For SQL Server

  For enabling notifications for SQL Server you need to follow these steps:
  1. Create a table 'ncache_db_sync' having four fields (cache_key VARCHAR, cache_id VARCHAR, modified BIT and work_in_progress BIT). Script to create the table is as follows:[SQL]
    --Create table'ncache_db_sync'
    CREATE TABLE ncache_db_sync (
    cache_key VARCHAR ( 256 ),
    cache_id VARCHAR ( 256 ),
    modified BIT DEFAULT ( 0 ),
    work_in_progress BIT DEFAULT ( 0 ),
    PRIMARY KEY ( cache_key , cache_id ) );
  2. Create UPDATE and DELETE triggers, for every table on which notification is required. They set the 'modified' field of corresponding row in the ncache_db_sync table to 1.To carry out the task, see the following sample script that creates trigger on 'Products' table:
    --create trigger
    CREATE TRIGGER myTrigger
    ON dbo . Products
    UPDATE ncdbs
    SET modified = 1
    from ncache_db_sync ncdbs
    inner join Deleted old on cache_key = (Cast((old.ProductID) as VarChar)
      + ':dbo.Products' );
Note: cache_key must be the same key that is used to add the corresponding record in the cache.
Now, As soon as an item is added to cache with dependency, a row will be created in table 'ncache_db_sync' for this cache key. We'll have to make sure that the format of cache key while adding into cache is exactly same as defined in the corresponding trigger. For our example, cache key for product id 10 should be like, "10:dbo.Products". Following code demonstrates how to add items with dependency.
  1. Create a new Clustered Cache or a Local Cache in NCache Manager with the name "sqlDependencyCache".
  2. Start the cache "sqlDependencyCache".
  3. Create a Java project in any Java environment.
  4. Add reference of NCClient from installationFolder../NCache/java/lib/
  5. Add the following import statement in your project:
    import com.alachisoft.ncache.web.caching.*;
    import com.alachisoft.ncache.runtime.dependencies.*;
  6. 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");
  7. 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;";
  8. Create a dependency and add 10 items to the cache
    for ( int i = 0 ; i < 10 ; i++)
    CacheDependency dependency = DBDependencyFactory.CreateSqlCacheDependency(connectionString, Integer.toString(i) + ":dbo.Products");
    _cache.insert(i + ":dbo.Products", i,dependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,CacheItemPriority.Default);
  9. Now update the key "6" of the Database table using the following SQL command:
    UPDATE Products SET ProductName = 'Modified Product' WHERE ProductID =6
  10. This invalidates cacheKey cache_key 6:dbo.Products and is removed by NCache in ncache_db_sync table.
See Also
Copyright © 2005-2012 Alachisoft. All rights reserved.