Using Cache Startup Loader
|
This feature is not available in NCache Express and Professional edition.
|
Namespace
For enabling Cache Startup Loader, your program needs a reference of the assembly Alachisoft.NCache.Runtime.dll that contains interface Alachisoft.NCache.Runtime.CacheLoader.ICacheLoader provided by NCache. The class implementing this interface allows framework to load data from the master datasource in the cache. Thus, the class needs to include the logic for loading the data from the datasource.
Following namespaces are required in your project for using ICacheLoader:
using Alachisoft.NCache.Runtime.CacheLoader;
using Alachisoft.NCache.Runtime.Caching;
ICacheLoader Interface:
In order to use ICacheLoader in your application, implement the following interface:
public void Init(System.Collections.IDictionary parameters);
public bool LoadNext(ref System.Collections.Specialized.OrderedDictionary data, ref object index);
public void Dispose();
Member
|
Description
|
Init
|
This method is called by NCache framework on cache startup.This method takes as input a dictionary of parameters which can be passed while configuring cache loader through NCache Manager. Please refer to how to configure cache loader section for details. Through this input parameter, runtime parameters to your deployed provider like connection strings etc can be specified. In this method tasks such as allocating resources, acquiring connections etc can be performed. The main concept of this method is to provide user with a starting point where he/she can initialize the data source settings before loading the data from it.
|
Dispose
|
This method is called by NCache framework on cache stop.The purpose of this method is to release resources that may be in use during loading data from data source to the cache.For example, when cache is stopped or LoadNext returns false, then this method can be implemented to close its connection with the datasource and dispose all resources in use.
|
LoadNext
|
This method is called by NCache framework on cache startup after init method. This method should contain the logic to load object(s) from the master data source. This method accepts a System.Collections.Specialized.OrderedDictionary which is to be populated bythe data being collected from the data source and an object which indicates the index. Both these parameters are passed by reference. The data loaded in the dictionary would be loaded in the cache and the index can be used to track the index if data is added in chunks. Please note that this method can contain the logic for returning one item at a time or multiple items in a chunk as it is called iteratively by NCache until it keeps returning false. Index can be used to control the size of the chunks being extracted from the data source. The user needs to make sure that the method returns true when the all the data has been added into the dictionary or else it will form an infinite loop.
|
Enabling Cache Startup Loader
Implementing Cache Startup Loader
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 System.Collections.Specialized.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();
product.Category = reader["Category"].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