Sample Implementation of ICacheLoader on Single Node
Important
Make sure to deploy the Cache Loader after implementation using NCache Manager as explained in Administrators’ Guide.
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,
To utilize the following APIs, include the following namespace 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
}
}
See Also
Components of Cache Startup Loader
Sample Implementation of ICacheLoader with Distribution Hints