Bulk Extensible Dependency Usage and Implementation
Note
This feature is only available in NCache Enterprise Edition.
Kindly refer to Custom Cache Dependencies to get acquainted with all cache dependency custom methods provided by NCache.
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. To implement Bulk Extensible Dependency in your application, use the following code snippet.
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.
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.
- To override the
BulkExtensibleDependency
class, include the following assembly in your application:Alachisoft.NCache.Runtime.Dependencies
- The class implementing
BulkExtensibleDependency
interface must be marked asSerializable
along with any other parameters the process code might take in.
[Serializable]
class OutOfStockProductsDependency : BulkExtensibleDependency
{
private string _connString;
private int _productID;
public int ProductId
{
get;
internal set;
}
public OutOfStockProductsDependency(string connString, int ProductID)
{
_connString = connString;
_productID = ProductID;
}
public override bool Initialize()
{
return true;
}
public override void EvaluateBulk(IEnumerable<BulkExtensibleDependency> dependencies)
{
List<int> ProductIds = GetOutofStockProducts();
IEnumerator<BulkExtensibleDependency> ienum = dependencies.GetEnumerator();
while (ienum.MoveNext())
{
var mydep = ienum.Current as OutOfStockProductsDependency;
if (ProductIds.Contains(mydep.ProductId))
mydep.Expire();
}
}
private List<int> GetOutofStockProducts()
{
List<int> ProductIDs = new List<int>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
command.CommandText = String.Format(CultureInfo.InvariantCulture,
"Select ProductID From Products" +
" where UnitsInStock = {0}", 0);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
ProductIDs.Add(Convert.ToInt32(reader["ProductID"].ToString()));
}
}
}
return ProductIDs;
}
// 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: Deploy Implementing on Cache
Deploy this class and any other dependent assemblies on NCache by referring to Deploy Providers in Administrator's Guide for help.
Step 3: Use Bulk Extensible Dependency
Once 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:
Pre-Requisites for using Bulk Extensible Dependency
- To utilize the
Insert
API, include the following namespaces in your application:Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Dependencies
Alachisoft.NCache.Runtime.Exceptions
- The dependency class being used 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
try
{
// Specify the connection string
string connectionString = ConfigurationManager.AppSettings["connectionstring"];
// Fetch the product to be added to the cache
Product product = FetchProductFromDB(1001);
// Specify the unique key of the item
string key = $"Product:{product.ProductID}";
// Create a cacheItem
var cacheItem = new CacheItem(product);
//Creating bulk dependency
BulkExtensibleDependency bulkDependency = new BulkExtensibleDependency(1001, connectionString);
cacheItem.Dependency = bulkDependency;
// 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