NCache 4.6 - Online Documentation

Sample Implementation of ICacheLoader Interface on Single Node

 
The following implementation adds 500 items of the Customer class as a bulk into the cache and then adds the remaining 500 (with key dependency) sequentially.
 
In order to use Cache Loader, make sure that the
  • firewall is not enabled
  • NCache Loader Service is running.
 
      To utilize the ICacheLoader, include the following namespaces in your application:
 
  • Alachisoft.NCache.Runtime.CacheLoader
  • Alachisoft.NCache.Runtime.Caching
 
 
public class Loader : ICacheLoader
{   
    private SqlConnection connection;
    private int _dependencyCount = 500; //first 500 items to be added in bulk
    private int _totalCount=1000;
    private int _index = 0;
 
    //initializing data source settings
    void Init(IDictionary parameters,string cacheId)
    {
        string connString = parameters["connectionString"].ToString();
        connection = new SqlConnection(connString);
        connection.Open();
    }
 
    //load data from source into cache
    LoaderResult LoadNext(object index)
    {
        LoaderResult result = new LoaderResult();
            
        int nextIndex = 0;
        if (index != null)
            nextIndex = (int)index;
            
        for (; nextIndex < _totalCount; nextIndex++)
        {
            Customer customer = new Customer(nextIndex, "name:" + nextIndex.ToString());
 
            ProviderCacheItem cacheItem = new ProviderCacheItem(customer);
 
            if (nextIndex < DependencyCount)
            {
                result.HasKeyDependency = false; //add in bulk
            }
            else if (nextIndex < DependencyCount * 2 && nextIndex > DependencyCount)
            {
                result.HasKeyDependency = true; //add sequentially
                cacheItem.Dependency = new KeyDependency("Customer: 0");
            }
            result.Data.Add("Customer: " + nextIndex, cacheItem);
        }
 
        result.HasMoreData = true;
        result.UserContext = nextIndex;
 
        return result;
    }
    void Dispose()
    {
        //optional implementation
    }
}
 
 
Make sure to deploy the Cache Loader after implementation using NCache Manager or command line tools.
 
See Also