NCache 4.6 - Online Documentation

Sample Implementation of ExtensibleDependency Class

 
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.
 
      To utilize the ExtensibleDependency abstract class, include the following namespace in your application: Alachisoft.NCache.Runtime.Dependencies.
 
[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.
 
 
See Also