NCache 4.4 - Online Documentation

Using Named Tags

 
NamedTagsDictionary class will be used to define Named Tags which will be added with the cache items for further use. Only string is allowed as a Named Tag “keys” and primitive data types are allowed as valid Named Tag “values”.
 
To use the Named Tags,the following namespace should be added in project:
 
 using Alachisoft.NCache.Runtime.Caching;
 
Creating Named Tags
 
A NameTagsDictionary is a class provided by NCache so that these tags can be associated with a particular data.
 
// Creating NameTagsDictionary and adding custom named tags.
NamedTagsDictionary productNamedTag = new NamedTagsDictionary();
productNamedTag.Add("UnitsAvailable", 4);
productNamedTag.Add("Category", 25);
productNamedTag.Add("Supplier", "Alex");
 
Adding Items to Cache with Named Tags
 
In this example, Add Operation overload is used to associate named tags previously created.
 
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
 
string key = "Product:" +product.ProductID ;
try
{
    cache.Add(key, product, productNamedTag);
}
catch (OperationFailedException ex)
{
    // handle exception
}
 
Adding Named Tags through CacheItem
 
In this example, named tags are set by assigning them as a property of CacheItem.
 
CacheItem cacheItem = new CacheItem(product);
cacheItem.NamedTags = productNamedTag;
try
{
    cache.Add(key, cacheItem);
}
catch (OperationFailedException ex)
{
    // handle exception
}
 
Querying Cached Items with respect to Named Tags
 
string queryString = "SELECT Product WHERE this.UnitsAvailable = ? AND this.Supplier = ?";
Hashtable values = new Hashtable();
values.Add("UnitsAvailable", 4);
values.Add("Supplier", "Alex");
try
{
    //if only related cache keys are required than use Search
    ICollection keysResultSet = cache.Search(queryString, values);
    // if both keys and values are required than use SearchEntries
    IDictionary resultSet = cache.SearchEntries(queryString, values);
    if (resultSet.Count> 0)
    {
        //Iterate through list of all products having UnitsAvailable as 4 and Supplier name is "Alex"
        IDictionaryEnumerator ide = resultSet.GetEnumerator();
        while (ide.MoveNext())
        {
            String key = (String)ide.Key;
            String value = (String)ide.Value;
            //...
        }
    }
}
catch (OperationFailedException ex)
{
    // handle exception
}
 
Here, the fully qualified name of the custom class should be specified instead of 'Product' in query string. If Named Tags are associated with “int” or any other data type then specific syntax to that type will be used. For example, for values of "int" data type "SELECT System.int32 WHERE this.UnitsAvailable = ?" will be used to query Named Tags.
 
If you have multiple applications that are sharing same cache and all of them are supposed to add named tags than make sure that same named tags have homogenous data types, e.g., if one client is adding named tag "ProductID" with String data type than all other clients should add values of "ProductID" in String format not in Integer or other for same cache.
 
 
 
See Also