Bulk Extensible Database Dependency Usage and Implementation [Deprecated]
In Extensible Dependency, each dependency takes time to determine change which might cause an 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.
Note
This feature is only available in the NCache Enterprise for .NET Servers.
To counter this issue, NCache provides another class called BulkExtensibleDependency, where a bulk of custom dependencies and database 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 the user can evaluate data and database dependency in bulk and call Expire on the dependencies that need to be removed from the cache.
Important
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.
Tip
Refer to Custom Cache Dependencies to get acquainted with all cache dependency custom methods provided by NCache.
Prerequisites
- Install the Alachisoft.NCache.SDK NuGet package in your application.
- The project must be implemented as a Class Library (.dll) in Visual Studio. This will be deployed on the NCache cluster.
- The class implementing
BulkExtensibleDependencymust be marked as Serializable, along with any parameters the process code might be taking in. - The
Dependencyclass must be deployed on cache. - For API details, refer to: ICache, CacheItem, Add, insert, ExecuteReader, CustomDependency, BulkExtensibleDependency, EvaluateBulk, CreateDependency, IList, ICustomDependencyProvider.
Step 1: Implementing the BulkExtensibleDependency Class
The first step in introducing your own logic for Bulk Extensible Database Dependency is to inherit the BulkExtensibleDependency class.
The following class implements your custom logic to remove multiple items at a time using Bulk Extensible Dependency.
[Serializable]
public class BulkDependency : BulkExtensibleDependency
{
// Class parameters
public BulkDependency(string connString, int ProductId)
{ try
{
connection = DriverManager.GetConnection(connString);
connection.open();
productID = ProductId;
}
catch (Exception ex)
{
// Throwing OperationFailedException allows NCache to log the failed initialization
throw new OperationFailedException("BulkDependency: Failed to initialize database connection.", ex);
}
}
private Dictionary<int,int> UnitsInStockStatus(List<int> productIds)
{
try
{
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;
}
catch (Exception ex)
{
throw new OperationFailedException("BulkDependency: Error retrieving status from database.", ex);
}
}
public override void EvaluateBulk(IEnumerable<BulkExtensibleDependency> dependencies)
{
try
{
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();
}
}
}
}
catch (Exception ex)
{
// Logging the failure of the bulk evaluation to NCache Service logs
throw new OperationFailedException("BulkDependency: Bulk evaluation failed.", ex);
}
}
private List<int> GetOutofStockProducts()
{
List<int> productIds = new List<int>();
try
{
foreach(BulkDependency bulkDependency in dependencies)
{
productIds.Add(bulkDependency.productId);
}
}
catch (Exception ex)
{
// This is where you handle the error
// Option A: Log and re-throw for NCache logs
throw new OperationFailedException("Error iterating dependencies", ex);
// Option B: Or, if you want to return the empty list on error:
// return productIds;
}
return productIds;
}
private Dispose()
{
try
{
if (connection != null)
{
connection.close();
}
}
catch (Exception)
{
// Fail silently during resource cleanup to prevent service disruption
}
}
// This class is to be deployed on NCache
}
Important
Do not 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()
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Step 2: Implement BulkCustomDependencyProvider
To implement a Bulk Extensible Database Dependency provider in your application, use the following code snippet.
A Custom Dependency Provider for Bulk Extensible Dependency implements the logic that you created in Step 1 on the server side. Here's how you can implement your own provider:
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 all other dependent assemblies on NCache by referring to Deploy Providers in the Administrator's Guide for help.
Step 4: Use Bulk Extensible Dependency
Once the Bulk Extensible Dependency class has been implemented and deployed, it is ready to be used in your application.
The following code shows how to add data into the cache using the Insert() with Bulk Dependency.
// 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);
Additional Resources
NCache provides a sample application for Bulk Extensible Dependency on GitHub.
See Also
.NET: Alachisoft.NCache.Runtime.Dependencies namespace.