Retrieving Previously Tagged Data
To utilize the APIs, include the following namespace in your application:
Alachisoft.NCache.Runtime.Caching.
Retrieving Keys Associated with a Tag
The following example demonstrates how to retrieve keys from the cache, either related to a specified tag, any tag from the list, or all the tags in the list.
//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 Key from Cache Containing Tag
The following examples demonstrate how to retrieve items (keys and values) from the cache, either containing a specified tag, any tag from the list, or all the tags in the list.
//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
To utilize the APIs, include the following namespace in your application:
Alachisoft.NCache.Runtime.Caching.
In the following code example, please specify the fully qualified name of your custom class instead of Product in the query string.
Hashtable queryValue = new Hashtable();
string query = "SELECT Product WHERE this.$Tag$=? AND this.$Tag$=?";
//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{
ICacheReader queryResult = cache.ExecuteReader(query, queryValue, true);
// queryResult contains all keys related to both tags.
if (queryResult.FieldCount > 0){
while (queryResult.Read()){
//perform operations
}
}
else{
//no record exists
}
}
catch (OperationFailedException ex){
// handle exception
}