NCache 4.4 - Online Documentation

Adding Data with File and Key Dependencies

 
Data in cache can depend on items like a file on some location, a record in database, results of a particular query or another cached item, such that any change in the item which has dependent data in cache invalidates the data. In that case all dependent data must be reloaded or removed from cache otherwise data will become stale.
 
Adding Data with Key Dependency
 
An object can be specified as being dependent on another key at the time of addition. Note that the only keys present in cache can be specified as key dependency. Nonexistent key specified as key dependency will result OperationFailedException.
 
The following code shows how to add an item with key dependency:
 
// precondition : Item with key "Product:1001" already exists in cache
Product product=new Product();
product.ProductID=1002;
product.ProductName="Chang";
 
string key1="Product:1001";
string key2="Product:"+product.ProductID;
try
{
//Adding cache item "Product:1002" with KeyDependency on "Product:1001"
cache.Insert(key2, product, new KeyDependency(key1), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal);
 
//Removing/Deleting/Updating key: "Product:1001" will automatically make all of its dependant keys removed, here it's "Product:1002"
}
catch (OperationFailedException ex)
{
    // handle exception
}
 
In Key Dependency an item can only depend on an existing item, thus all dependency keys must exist in cache before an item can depend on them.
 
Adding Data with File Dependency
 
Following code shows how to add data with File Dependency.
 
Product product=new Product();
product.ProductID=1001;
product.ProductName="Chai";
 
string key="Product:"+product.ProductID;
string filepath = "C:\\tempProductList.txt";
 
try
{
    //Adding cache item "Product:1001" with FileDependency on file "tempProductList.txt"
    cache.Insert(key, product, new FileDependency(filepath), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal);
}
catch (OperationFailedException e)
{
    // handle exception
}
//Modifying file: "Product:1001" will automatically removed
 
 
See Also