Alachisoft NCache 4.1 - Online Documentation

Named Tags

NCache provides feature of “Named Tags” through which user will be able to associate keyword(s) with the 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 a 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 supposed 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 import statements in your project:
import com.alachisoft.ncache.runtime.caching.NamedTagsDictionary;
Initialize cache and Named Tags:
            Cache mycache = NCache.initializeCache("mycache");
      NamedTagsDictionary nameTag1 = new NamedTagsDictionary();
      nameTag1.add("Name", "Alex");
      nameTag1.add("Age", 25);
      nameTag1.add("Salary", 10000.50);
Add item to cache with Named Tags:
mycache.add("key1", "Customer", nameTag1);
Insert item to cache with Named Tags:
mycache.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 = ?";
HashMap values = new HashMap();
values.put("Age", 10);
values.put("Name", "alex");
values.put("Salary", 10000.25);
HashMap dict = _cache.searchEntries(queryString, values);
if (dict.size() > 0)
    Iterator ide = dict.entrySet().iterator();
    while (ide.hasNext())
        Map.Entry entry = (Map.Entry);
        String key = (String) entry.getKey();
        Person prs = (Person) entry.getValue();
        System.out.println("Key = " + key + ", Age: " + prs.Age);
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 © 2005-2012 Alachisoft. All rights reserved.