NCache 4.4 - Online Documentation

Custom Dependency

 
In custom dependency, the user can have his own dependencies scenarios. In this way, items can be be expired from cache in a number of flexible ways where expiration logic meets the user’s business requirements. NCache provides an abstract class ExtensibleDependency that is base for all dependencies.
 
How to Implement Custom Dependency
 
For overriding ExtensibleDependency class, user’s program needs a reference to the assembly Alachisoft.NCache.Runtime.dll that contain abstract class Alachisoft.NCache.Runtime.Dependencies.ExtensibleDependency provided by NCache. ExtensibleDependency class contains following abstract method and property.
 
public abstract bool HasChanged { get; }
public abstract bool Initialize();
 
Method
Description
Public bool Initialize();
This method can be used to perform tasks like allocating resources, acquiring connections etc. If resources has been successfully acquired, return true else false. If Initialize return false NCache removes item with custom dependency from the cache.
 
Public bool HasChanged { get;}
NCache calls HasChanged (property) after every clean interval. If HasChanged returns true all custom dependent items will be removed from cache.  
 
Follow the following steps to implement custom dependency.
 
Step 1: Inherit the dependency class from ExtensibleDependency and override the abstract function and property.
 
Lets suppose there is a grocery store which has a big collection of those products which customer often buy. These products can be cached from the database to check their units in stock.The dependency polls the Northwind database and expires all the Products for which units in stock are less than 100.
 
[Serializable]
public class CustomDependency :ExtensibleDependency
{
private string _connString;
private int _productID;
 
public override bool Initialize()
    {
return true;
    }
 
public CustomDependency(int productID, string connString)
    {
        _connString = connString;
        _productID = productID;
    }
 
internal bool DetermineExpiration()
    {
if (GetAvailableUnits(_productID) < 100)
return true;
return false;
    }
 
internal int GetAvailableUnits(int productID)
    {
SqlDataReader reader = null;
SqlConnection connection = new SqlConnection(_connString);
connection.Open();
int availableUnits = -1;
try
        {
SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "Select UnitsInStockFrom Products where ProductID=" + productID + "";
reader = cmd.ExecuteReader();
if (reader.Read())
            {
availableUnits = Convert.ToInt32(reader["UnitsInStock"].ToString());
            }
reader.Close();
return availableUnits;
        }
catch (Exception)
        {
return availableUnits;
        }
    }
 
public override bool HasChanged
    {
get
        {
return DetermineExpiration();
        }
    }
}
 
Step 2:  Place assembly or executable file of class overriding ExtensibleDependency in NCache service folder and restart the NCache service.
 
Adding Data with Custom Dependency
 
Once custom dependency class has been implemented and deployed, its ready to be used in your program. Following code shows how to use custom dependency:
 
string connString ="Provider=SQLOLEDB.1;Data Source=localhost;User ID=sa;password=;Initial Catalog=Northwind";
//Creating custom dependency
CustomDependency hint = new CustomDependency (123, connString);
Product product=new Product();
product.ProductID=1001;
product.ProductName="Chai";
string key="Product:"+product.ProductID;
try
{
//Adding data with custom dependency
    cache.Insert(key, product, new CacheDependency(hint), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, Alachisoft.NCache.Runtime.CacheItemPriority.Default);
}
catch (OperationFailedException exp)
{
    // handle exception
}
 
 
 
See Also