Alachisoft NCache 4.1 - Online Documentation

DBCache Dependency

NOTE: This feature is not available in NCache Express and Professional edition.
DBCacheDependency is used to keep the cache items synchronized with any of the database. Prior to its use, you need to enable notifications for the underlying database. For details, see Enabling Notifications For SQL Server, Enabling Notifications For DB2 and Enabling Notifications For Oracle. This is a row based dependency. Cache key is associated with primary key value of the table. Thus an item expires only if the row with that primary key gets changed.
While using DBCacheDependency for SQL Server, you need to use 'Sql versions' of commands, connections, readers etc. available under 'System.Data.SqlClient' namespace in your client application. For Oracle and DB2, you need to use 'OleDb versions' of commands, connections, readers etc., available under 'System.Data.OleDb' namespace.
Following is the step-by-step guide for using DBCacheDependency. The example given is for SQL Server, however, Oracle and DB2 databases can also be used with little modifications.
  1. Enable Notifications for Products table in Northwind database. See Enabling Notifications For SQL Server.
  2. Create an ASP.NET Application inside Visual Studio.NET by the name DataCachingUsingNCache.
  3. Add a new Web Form named DataCacheSample.aspx to your project and include the following namespaces.
    using Alachisoft.NCache.Web.Caching;
    using Alachisoft.NCache.Runtime;
    using Alachisoft.NCache.Runtime.Dependencies;
  4. Add a nested class Product inside the code behind.
    class Product
  5. Declare a private variable "_cache" in the code behind of the DataCacheSample as.
    private Cache _cache;
  6. Add the following code in Page_Load event of the DataCacheSample as.
    protected void Page_Load( object sender, EventArgs e)
    if (_cache == null ) { _cache = NCache.InitializeCache( "mycache" ); }
  7. On the Design view tab for the Web Form, add a Web Form button to the DataCacheSample page. Change the ID property of this button to FetchFromDB, and then change the Text property to Fetch From Database.
  8. Add a second Web Form button. Change the ID property to FetchFromCache, and then change the Text property to Fetch From Cache.
  9. Add a Web Form label. Change the ID property to Status, and then clear the Text property.
  10. On the Design view tab, double-click the FetchFromDB button to open the FetchFromDB_Click event in the code-behind page.
  11. Add the following code to the FetchFromDB_Click event:
    protected void FetchFromDB_Click(object sender, System.EventArgs e)
    string connectionString = "Data Source=local host; initial catalog=northwind;User ID = sa; password =;";
    string QueryString = "SELECT ProductID, ProductName, SupplierID, CategoryID, UnitPrice FROM dbo.Products where productid < 11";
    DataSet ds = new DataSet ();
    //Create the connection and pass in the ConnectionString
    SqlConnection mySqlConn = new SqlConnection(connectionString);
    SqlCommand sqlCmd = new SqlCommand(QueryString, mySqlConn);
    SqlDataReader sqlReader = sqlCmd.ExecuteReader();
    List <Product> products = new List <Product>();
    while (sqlReader.Read())
    Product product = new Product();
    product.ProductID = sqlReader.GetInt32(0);
    product.ProductName = sqlReader.GetString(1);
    product.SupplierID = sqlReader.GetInt32(2);
    product.CategoryID = sqlReader.GetInt32(3);
    product.UnitPrice = sqlReader.GetDecimal(4);
    DBCacheDependency dependency = DBDependencyFactory.CreateSqlCacheDependency(connectionString,"Primary Key:dbo.Products");
    //Set the DataGrid's DataSource to the "products" list
    DataGrid1.DataSource = products;
    //Display the status
    Status.Text = products.Count+ " products fetched from database.";
  12. On the Design view tab for the DataCacheSample.aspx page, double-click the FetchFromCache button to open the FetchFromCache_Click event in the code-behind page.
  13. Add the following code to the FetchFromCache_Click event:
    protected void FetchFromCache_Click(object sender, EventArgs e)
    List<Product> products = new List<Product>();
    for (int i = 0; i < 11; i++)
    object cachedProduct = _cache.Get(i + ":dbo.Products");
    if(cachedProduct == null){ continue; }
    Product product = cachedProduct as Product;
    DataGrid1.DataSource = products;
    Status.Text = products.Count + " products fetched from cache.";
  14. On the Build menu in the Visual Studio .NET IDE, click Build to build the project.
  15. To run the code, right-click the DataCacheSample.aspx page in Solution Explorer, and then click View in Browser.
  16. Click the FetchFromDB button; the Status label should display "10 products fetched from database." and the DataGrid control is populated. All products are stored in cache.
  17. Click the FetchFromCache button; the Status label should display "10 products fetched from cache." and the DataGrid control is populated.
  18. Modify one of the displayed records in the Northwind database by using SQL Query Analyzer or another tool. After you modify the record, cached data for that row is invalidated.
  19. Click the FetchFromCache button again. The Status label will display the message "9 products fetched from cache." and 9 products are populated. The modified record in database was removed from cache.
See Also
Copyright © 2005-2012 Alachisoft. All rights reserved.