Implement ICacheLoader with Distribution Hints
Note
This feature is only available in NCache Enterprise Edition.
To configure Cache Startup Loader, ICacheLoader interface needs to be implemented and then configured using NCache Web Manager. NCache uses this custom provider to load data from the configured data source either in bulk or sequentially in case of key dependency among the items. On cache startup, NCache framework iteratively calls the LoadNext() method of this custom provider until no more data remains.
For more detail on the components of Cache Loader, refer to the chapter Components of Cache Startup Loader.
Pre-Requisites
- To utilize the following APIs, include the following namespace in your application:
Alachisoft.NCache.Runtime.CacheLoader
Alachisoft.NCache.Runtime.Caching
Alachisoft.NCache.Runtime.Dependencies
Alachisoft.NCache.Runtime.Exceptions
- This should be a class library project using Microsoft Visual Studio.
- Make sure to configure the Cache Loader using NCache Web Manager.
The following implementation of ICacheLoader compares the distribution hints
and loads the respective data through querying. If the hint matches
Products
, LoadProductData()
fetches all products fulfilling the querying
criteria, which are then loaded into the cache through the node. Similar action
is taken for Customer and Orders, where the respective data is fetched and
loaded accordingly.
public class Loader : ICacheLoader
{
int ItemsPerHint = 10000;
private SqlConnection connection;
string _hint = null;
public void Init(IDictionary parameters, string cacheId)
{
try
{
IDictionaryEnumerator ide = parameters.GetEnumerator();
while (ide.MoveNext())
{
// Get connection string
if (ide.Key.ToString().Equals("connectionstring", StringComparison.OrdinalIgnoreCase))
{
string connString = ide.Value.ToString();
connection = new SqlConnection(connString);
connection.Open();
}
// Get distribution hints – distributionhint is a keyword
if (ide.Key.ToString().Equals("distributionhint",
StringComparison.OrdinalIgnoreCase))
{
_hint = ide.Value.ToString();
}
}
}
catch(Exception ex)
{
// Handle Exception
}
}
public LoaderResult LoadNext(object index)
{
if (_hint != null)
return LoadData(index);
else
return new LoaderResult();
}
protected LoaderResult LoadData(object index)
{
// Compare hints and load respective data
if (_hint == "products")
return LoadProductData(index);
else if (_hint == "customer")
return LoadCustomerData(index);
else if (_hint == "order")
return LoadOrderData(index);
else
return new LoaderResult();
}
protected LoaderResult LoadProductData(object index)
{
LoaderResult result = new LoaderResult();
try
{
int lastIndex = 0;
if (index != null)
{
lastIndex = (int)index;
}
// Fetch specific data based on query
var command = new SqlCommand("SELECT * FROM Products WHERE ProductID > " +
lastIndex.ToString() + " AND ProductID < " + (lastIndex + 10).ToString(), connection);
SqlDataReader reader = command.ExecuteReader();
int nextLimit = 50 + lastIndex;
while (reader.Read() && lastIndex < nextLimit && lastIndex < ItemsPerHint)
{
Product product = new Product();
product.ProductName = reader["ProductName"].ToString();
product.ProductID = Convert.ToInt32(reader["ProductID"]);
ProviderCacheItem cacheItem = new ProviderCacheItem(product);
string key = "Product: " + product.ProductID;
var keyvaluePair = new KeyValuePair<string, ProviderItemBase>(key, cacheItem);
result.Data.Add(keyvaluePair);
lastIndex++;
}
if (lastIndex < ItemsPerHint)
{
result.UserContext = lastIndex;
result.HasMoreData = true;
}
}
catch(Exception ex)
{
// Handle exception
}
return result;
}
protected LoaderResult LoadCustomerData(object index)
{
// Perform operations
return new LoaderResult();
}
protected LoaderResult LoadOrderData(object index)
{
// Perform operations
return new LoaderResult();
}
public void Dispose()
{
// Optional implementation
}
// Configure cache loader on cache
}
Important
Configure Cache Loader on NCache by referring to Configure Cache Loader in Administrator’s Guide for help.
See Also
Components of Cache Startup Loader
Sample Implementation of ICacheLoader on Single Node
Data Source Providers (Backing Source)
Custom Cache Dependencies