NCache 4.4 - Online Documentation

Tagging Cached Data

 
NCache provides with the facility of Tags which act as identifying marks for the cache items. Through tags, the user can associate string keywords(s) with cache items. You can also search and remove items from the cache by specifying tags.
 
Creating Tags
 
Tag is a class provided by NCache. This class can be used to associate tags with a particular data.
 
Tags are case sensitive.
 
// Create a Tag array and specify tags.
Tag[] productTags= new Tag[2];
productTags[0] = new Tag("Product");
productTags[1] = new Tag("Beverages");
 
Adding Items to Cache with Tags
 
In this example Add Operation overload is used to associate tags that were previously created.
 
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
string key = "Product:" + product.ProductID;
try
{
    cache.Add(key, product, productTags);
}
catch (OperationFailedException ex)
{
    // handle exception
}
 
Tagging Data through CacheItem
 
In this example tags are set by assigning them as a property of CacheItem.
 
CacheItem cacheItem = new CacheItem(product);
cacheItem.Tags = productTags;
try
{
    cache.Add(key, cacheItem);
}
catch (OperationFailedException ex)
{
    // handle exception
}
 
Retrieving Previously Tagged Data
 
  • Retrieving Keys Associated with a Tag
 
//For finding keys related to one tags
ICollection keys = cache.GetKeysByTag(productTags[0]);
//For Finding keys containing at least any one tag from tag list.
ICollection keys = cache.GetKeysByAnyTag(productTags);
//For finding keys containing all tags from tag list.
ICollection keys = cache.GetKeysByAllTags(productTags);
 
  • Retrieving Keys from Cache Containing Tag
 
//For finding keys and values related to one tag.
Hashtable result = cache.GetByTag(productTags[0]);
//For finding keys and values containing at least any one tag in the tag list
Hashtable result = cache.GetByAnyTag(productTags);
//For finding keys and values related to all tags from the tag list
Hashtable result = cache.GetByAllTags(productTags);
 
  • Querying Cached Items with respect to Tags
 
Hashtable queryValue = new Hashtable();
//Add items to cache containing tags in ArrayList if multiple tags value is required.
ArrayList queryTagList = new ArrayList();
queryTagList.Add("Product");
queryTagList.Add("Beverages");
// Add tag list to Hashtable for query searching values.
queryValue.Add("$Tag$", queryTagList);
try
{
    //Query string, for searching Cache items with respect to Tags.
    ICollection keysResultSet = cache.Search("SELECT Product WHERE this.$Tag$=? AND this.$Tag$=?", queryValue);
    // keysResultSet contains all keys related to both tags.
}
catch (OperationFailedException ex)
{
    // handle exception
}
 
Here the user needs to specify the fully qualified name of custom class instead of 'Product' in the query string. Only .NET clients can use System.String or System.Int32 etc related to cache value types.
 
  • Removing  Items from Cache Containing Tag
 
//For removing keys containing specified tag
cache.RemoveByTag(productTags[0]);
//For removing keys containing at least one tag from tags list.
cache.RemoveByAnyTag(productTags);
//For removing keys containing all tags from list
cache.RemoveByAllTags(productTags);
 
See Also