NCache 4.3 - Online Documentation

Named Tags

 
NOTE: This feature is not available in NCache Express edition.
 
NCache is providing feature “Named Tags” through which user will be able to associate one or more keywords with cache items. These keywords will act as an identifying mark for the cache items and will be used for querying the cache data.
 
"Named Tags" is the enhancement of "Tags". Consider a scenario where user needs to store an object in textual form such as "xml", he will not be able to search that object using tags. The limitation of tags is that it can only have text values and only one attribute i.e. $Tag$. With named tags user will be able to store additional information (of any type) required to query the object stored as string.
 
Here users are required to provide the list of named tags, each having two parameters, "key" (name of a tag) as string and "value" (assigned value) as any primitive type. NCache then allows you to search your objects through these 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, include the following namespace in your project:
 
using Alachisoft.NCache.Runtime.Caching;
 
Initialize cache and Named Tags:
 
Cache _cache = NCache.InitializeCache("mycache");
_cache.Clear();
 
NamedTagsDictionary nameTag1 = new NamedTagsDictionary();
nameTag1.Add("Name", "Alex");
nameTag1.Add("Age", 25);
nameTag1.Add("Salary", 10000.50);
 
Add item to cache with Named Tags:
 
_cache.Add("key-1", "Customer", nameTag1);
 
Insert item to cache with Named Tags:
 
_cache.Insert("key-1", "Employee", nameTag1);
 
Using Query string for searching cache items with respect to Named Tags:
 
string queryString = "SELECT $Text$ WHERE this.Age = ? AND this.Name = ? AND this.Salary = ?";
 
Hashtable values = new Hashtable();
 
values.Add("Age", 10);
values.Add("Name", "Alex");
values.Add("Salary", 10000.25);
 
IDictionary dict = _cache.SearchEntries(queryString, values);
 
if (dict.Count > 0)
{
    IDictionaryEnumerator ide = dict.GetEnumerator();
    while (ide.MoveNext())
    {
        String key = (String)ide.Key;
        Person prs = (Person)ide.Value;
        HandlePerson(prs);
        Console.WriteLine("Key = {0}, Age: {1}", key, prs.Age);
    }
}
 
 
Note:
 
Here $Text$ represents the textual data in a cache. If Named Tags are associated with some string value then this syntax can be used in queries. $Text$ is independent of client and can return values added by both .Net and Java clients. If Named Tags are associated with “int” or any other data type then specific syntax to that type will be used. For example for "int" data type System.Int32 will be used to query Named Tags.
 
 
See Also
 
© Copyright  2014 Alachisoft. All rights reserved.