Bulk Extensible Dependency Usage and Implementation
Note
This feature is only available in NCache Enterprise Edition.
In extensible dependency, each dependency takes time to determine change which might cause expiration lag. With the increase in the number of custom dependencies, expiration on each item takes time because of which some items remain in the cache way past their expiry.
In addition to Extensible Dependency, NCache provides another class called BulkExtensibleDependency
. Here, a bulk of custom dependencies are evaluated at the same time. This helps boost the application's performance as the items that have expired are removed from the cache in time.
BulkExtensibleDependency
is provided at runtime through which user can evaluate data dependencies in bulk and call .Expire()
on the dependencies that need to be removed from the cache.
Note
- In case your business requirement is a method where an item needs to be individually evaluated instead of in a bulk, then use
ExtensibleDependency
. Otherwise, you will face performance issues. - Kindly refer to Custom Cache Dependencies to get acquainted with all cache dependency custom methods provided by NCache.
Step 1: Implement BulkExtensibleDependency Class
Pre-Requisites
- The class must be implemented as a Class Library (.dll) in Visual Studio. This will be deployed on the NCache cluster.
- Install the following NuGet packages in your application:
- To override the
BulkExtensibleDependency
class, include the following assembly in your application:Alachisoft.NCache.Runtime.Dependencies
- The class implementing
BulkExtensibleDependency
must be marked asSerializable
along with any other parameters the process code might take in.
[Serializable]
public class BulkDependency : BulkExtensibleDependency
{
// Class parameters
public BulkDependency(string connString, int ProductId)
{
connection = DriverManager.GetConnection(connString);
connection.open();
productID = ProductId;
}
private Dictionary<int,int> UnitsInStockStatus(List<int> productIds)
{
String queryString = "your_query_here";
Dictionary<int, int> productsInfo = new Dictionary<int, int>();
using (var cmd = new SqlCommand(queryString, connection))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int units = 0;
Int32.TryParse(reader["UnitsInStock"].ToString(), out units);
productsInfo.Add((int)reader.GetValue(0), units);
}
}
}
return productsInfo;
}
}
public override void EvaluateBulk(IEnumerable<BulkExtensibleDependency> dependencies)
{
List<BulkDependency> dependencyList = new List<BulkDependency>();
foreach (BulkDependency bulkDependency in dependencies)
{
dependencyList.Add(bulkDependency);
}
List<int> productIds = GetOutofStockProducts(dependencyList);
var productsInfo = UnitsInStockStatus(productIds);
foreach (BulkDependency bulkDependency in dependencyList)
{
if (productsInfo.ContainsKey(bulkDependency.productId))
{
if (productsInfo[bulkDependency.productId] != bulkDependency.unitsInStock)
{
bulkDependency.Expire();
}
}
}
}
private List<int> GetOutofStockProducts()
{
List<int> ProductIDs = new List<int>();
foreach(BulkDependency bulkDependency in dependencies)
{
productIds.Add(bulkDependency.productId);
}
return productIds;
}
return ProductIDs;
}
private Dispose()
{
// Dispose off all resources
}
// This class is to be deployed on NCache
}
Note
Don't use this.Expire()
to expire an object that isn't valid anymore. Instead, call the .Expire()
method on the instance of dependency that needs to be expired. e.g. myDep.Expire()
Step 2: Implement BulkCustomDependencyProvider
To implement Bulk Extensible Dependency provider in your application, use the following code snippet.
Pre-Requisites
- Install the following NuGet packages in your application:
- To utilize the APIs, include the following namespaces in your application:
Alachisoft.NCache.Runtime.Dependencies
Alachisoft.NCache.Runtime.CustomDependencyProviders
Alachisoft.NCache.Runtime
Alachisoft.NCache.Runtime.Exceptions
- This should be a class library project using Microsoft Visual Studio.
public class BulkCustomDependencyProvider : ICustomDependencyProvider
{
public void Init(IDictionary<string, string> parameters, string cacheName)
{
// Initialize cache and class parameters
}
public BulkDependency CreateDependency(string key, IDictionary<string, string> dependencyParameters)
{
int productId = 0;
int units = 0;
string connectionString = "";
if (dependencyParameters != null)
{
if (dependencyParameters.ContainsKey("ProductID"))
productId = Int32.Parse(dependencyParameters["ProductID"]);
if (dependencyParameters.ContainsKey("UnitsAvailable"))
units = Int32.Parse(dependencyParameters["UnitsAvailable"]);
if (dependencyParameters.ContainsKey("ConnectionString"))
connectionString = dependencyParameters["ConnectionString"];
// Create bulk extensible dependency
BulkDependency dependency = new BulkDependency(connectionString, productId, units);
return dependency;
}
}
public void Dispose ()
{
// Dispose off all resources
}
}
Step 3: Deploy Implementation on Cache
Deploy this class and any other dependent assemblies on NCache by referring to Deploy Providers in Administrator's Guide for help.
Step 4: Use Bulk Extensible Dependency
Once bulk extensible dependency class has been implemented and deployed, it is ready to be used in your application.
Pre-Requisites for using Bulk Extensible Dependency
- Install the following NuGet packages in your application:
- To utilize the
Insert
API, include the following namespaces in your application:Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Dependencies
Alachisoft.NCache.Runtime.Exceptions
- The
BulkExtensibleDependency
class must be deployed on cache. - The application must be connected to cache before performing the operation.
- The cache must be running.
- To ensure the operation is fail-safe, it is recommended to handle any exceptions within your application, as explained in Handling Exceptions
The following code shows how to add data into the cache using the Insert() with bulk dependency:
try
{
// Specify the connection string
string connectionString = ConfigurationManager.AppSettings["connectionstring"];
// Fetch the product to be added to the cache
Product product = FetchProductFromDB(productId);
// Specify the unique key of the item
string key = $"Product:{product.ProductID}";
// Create a cacheItem
var cacheItem = new CacheItem(product);
// Create dictionary for dependency parameters
IDictionary<string, string> param = new Dictionary<string, string>();
param.Add("ProductID", products.Id.ToString());
param.Add("ConnectionString", _connectionString);
// Create Bulk Extensible Dependency using Provider and Add it to Cache Item
CustomDependency customDependency = new CustomDependency(ConfigurationManager.AppSettings["ProviderName"], param);
cacheItem.Dependency = customDependency;
// Add cacheItem to the cache with bulk dependency
cache.Insert(key, cacheItem);
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Additional Resources
NCache provides sample application for bulk extensible dependency on GitHub.
See Also
Custom Dependencies
Sync Cache using Extensible Dependency
Sync Cache using Notify Extensible Dependency
Configure Custom Dependencies