Sample Usage of Custom Dependency
Once custom 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 with custom dependency:
To utilize the Insert API, include the following namespace in your application:
Alachisoft.NCache.Web.Caching
.
string connString = "Provider=SQLOLEDB.1; Data Source=localhost; User ID=sa; password=; Initial Catalog=Northwind";
//Creating custom dependency
CustomDependency customDependency = new CustomDependency(123, connString);
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
string key = "Product:" + product.ProductID;
try
{
//Adding data with custom dependency
cache.Insert(key, product, new CacheDependency(customDependency), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default);
}
catch (OperationFailedException exp)
{
// handle exception
}