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 custom dependency, the users can have their own dependencies scenarios. In this way, items can be expired from cache in a number of flexible ways where expiration logic meets the user’s business requirements. NCache provides an abstract class ExtensibleDependency that is base for all dependencies.
Step 1: Implement ExtensibleDependency Class
Pre-Requisites
- The class must be implemented as a Class Library (.dll) in Visual Studio. This will be deployed on NCache cluster.
- To override the
ExtensibleDependency
class, include the following assembly in your application:Alachisoft.NCache.Runtime.Dependencies
- The class implementing
ExtensibleDependency
interface must be marked asSerializable
, along with any parameters the process code might be taking in.
Let's suppose there is a grocery store which has a big collection of those products which customers often buy. These products can be cached from the database to check their units in stock. The dependency polls the Northwind database and expires all the Products for which units in stock are less than 100.
[Serializable]
public class Dependency : ExtensibleDependency
{
private string _connString;
private int _productID;
public override bool Initialize()
{
return true;
}
public Dependency(int productID, string connString)
{
_connString = connString;
_productID = productID;
}
internal bool DetermineExpiration()
{
if (GetAvailableUnits(_productID) < 100)
return true;
return false;
}
internal int GetAvailableUnits(int productID)
{
SqlDataReader reader = null;
var connection = new SqlConnection(_connString);
connection.Open();
int availableUnits = -1;
try
{
SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "Select UnitsInStockFrom Products where ProductID=" + productID;
reader = cmd.ExecuteReader();
if (reader.Read())
{
availableUnits = Convert.ToInt32(reader["UnitsInStock"].ToString());
}
reader.Close();
return availableUnits;
}
catch (Exception)
{
return availableUnits;
}
}
public override bool HasChanged
{
get
{
return DetermineExpiration();
}
}
// This class is to be deployed on NCache
}
Step 2: Deploy Implementation on Cache
Deploy this class and any dependent assemblies on NCache by referring to Deploy Providers in Administrator’s Guide for help.
Step 3: Use Extensible Dependency
Once 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 extensible dependency:
Pre-Requisites for Using 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
ExtensibleDependency
class being used must be deployed on cache. - The application must be connected to cache before performing the operation.
- Cache must be running.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
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 extensible dependency
Dependency dependency = new Dependency(1001, connectionString);
cacheItem.Dependency = dependency;
// Add cacheItem to the cache with 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 extensible dependency on GitHub.
See Also
How to Configure Custom Dependency
Cache Data Dependency on External Source
WAN Replication across Multi Datacenters through Bridge
Data Source Providers (Backing Source)