NCache 4.6 - Online Documentation

Sample Implementation of ICacheLoader Interface

 
Make sure to deploy the Cache Loader after implementation using NCache Manager.
 
      To utilize the ICacheLoader, include the following namespaces in your application:
  • Alachisoft.NCache.Runtime.CacheLoader
  • Alachisoft.NCache.Runtime.Caching
 
 
public class CacheLoader : ICacheLoader
    {
        private SqlConnection connection;
        
        //initialize your data source setting here
        public void Init(System.Collections.IDictionary parameters)
        {
            string connString = parameters["connectionString"].ToString();
 
            connection = new SqlConnection(connString);
 
            connection.Open();
        }
 
        //Load data from source
        public bool LoadNext(ref OrderedDictionary data, ref object index)
        {
            int nextIndex = 0;
            if (index != null)
            {
                nextIndex = (int)index;
            }
 
            SqlCommand command = new SqlCommand("SELECT * FROM Products WHERE ProductID > " + nextIndex.ToString()+ " AND ProductID< "                          +(nextIndex + 10).ToString());
 
            SqlDataReader reader = command.ExecuteReader();
 
            if (!reader.HasRows)
                return true;
 
            while (reader.Read())
            {
                Product product = new Product();
                product.ProductID = Convert.ToInt32(reader["ProductID"].ToString());
                product.ProductName = reader["ProductName"].ToString();
 
                ProviderCacheItem provideritem = new ProviderCacheItem(product);
 
                data.Add("Product:" + product.ProductID, provideritem);
            }
 
            index = nextIndex + 10;
 
            reader.Close();
 
            return false;
        }
 
        //Dispose your data source resources here
        public void Dispose()
        {
            if (connection != null)
                connection.Close();
        }
    }
 
 
See Also