NCache 4.3 - Online Documentation

Implementing ICacheLoader Interfaces

 
NOTE: This feature is not available in NCache Express and Professional edition.
 
Implementing ICacheLoader
 
Following is a list of methods needs to be defined by the class implementing interface and a brief description of their purpose.
 
  • void Init(IDictionary parameters);
 
This method performs tasks like allocating resources, acquiring connections etc. When the cache is initialized and Cache Startup Loader is enabled, it calls Init method to notify the client that cache has initialized and you may initialize your data source too. The parameters passed as an argument contains all the parameters (if any) that were specified using NCache Manager cache/cluster views. These parameters can be utilized in many ways. For example, connection string of a datasource can be specified as a parameter through NCache Manager. Whenever cache will be initialized, the connection to the datasource could be made using this connection string passed as an argument. Thus, it provides a flexible way to change the datasource dynamically without requiring code changes.
 
  • void Dispose();
 
This method performs tasks like releasing the resources etc. When Cache is stopped or LoadNext returns false, it calls the Dispose() method to notify the client that the task has completed or cache has stopped now and you can free the resources related to your datasource.
 
  • public bool LoadNext(ref System.Collections.Specialized.OrderedDictionary data, ref object index);
 
This method should contain logic to load object(s) from the master datasource. After Init call, framework calls LoadNext method in a loop till LoadNext returns false. This method receives an OrderedDictionary reference that user needs to set and populate with its respective data. Another ref variable is index. User must set it to be starting index in given OrderedDictionary. This function allows user to add data in chunks, or add it as a single operation. If user wants to add data in chunks, he must return True back to caller framework, indicating that this method should be called again. User should return false, to end Startup loading of data by the framework.
 
 
Using ICacheLoader:
 
Here is how your class should look like for ICacheLoader:
 
namespace CacheLoaderApp
{
    class CacheLoader : ICacheLoader
    {
        private string _connString = @"Server=stagingServer2; initial catalog=Northwind; User ID=sa; password=xxxxxx;";
        private SqlConnection _connection;
        #region ICacheLoader Members
        public void Init(System.Collections.IDictionary parameters)
        {
            _connection = new SqlConnection(_connString);
            _connection.Open();
        }
        public bool LoadNext(ref System.Collections.Specialized.OrderedDictionary data, ref object index)
        {
            int nextIndex = 0;
            if (index != null)
            {
                nextIndex = (int)index;
            }
            SqlCommand command = new SqlCommand("SELECT * FROM Customers WHERE CustomerID > " + nextIndex.ToString() + " AND CustomerID < " + (nextIndex + 10).ToString());
            SqlDataReader reader = command.ExecuteReader();
            if (!reader.HasRows)
                return true;
            while (reader.Read())
            {
                Customer customer = new Customer();
                customer.CustomerID = reader["CustomerID"].ToString();
                customer.ContactName = reader["ContactName"].ToString();
                customer.Country = reader["Country"].ToString();
 
                ProviderCacheItem provideritem = new ProviderCacheItem(customer);
 
                provideritem.Dependency = new KeyDependency("customer:1");
                provideritem.AbsoluteExpiration = System.DateTime.Now.AddSeconds(30);
           
                data.Add("Customer:" + customer.CustomerID, provideritem);
            }
   
            index = nextIndex + 10;
            reader.Close();
            return false;
        }
        public void Dispose()
        {
            if (_connection != null)
                _connection.Close();
        }
        #endregion
    }
}
 
NCache logs the warnings in Application event log in case of an exception during loading the assemblies.
For debuging your providers see How to Debug Providers.
 
 
 
See Also
 
© Copyright  2014 Alachisoft. All rights reserved.