NCache 4.3 - Online Documentation

Tags

 
NOTE: This feature is not available in NCache Express edition.
 
NCache provides you the facility of Tags which acts as an identifying mark for the cache items. Through tags, user can associate keywords(s) with cache objects. Moreover, user can get and remove items from the cache by tags.
 
Include the following namespace in your project before using Tags:
 
using Alachisoft.NCache.Runtime.Caching;
 
Initialize cache and tags:
 
Cache _cache = NCache.InitializeCache("mycache");
_cache.Clear();
Object valueToStore = "Can contain any type";
 
Tag [] tagList = new Tag [2];
tagList[0] = new Tag ("myTag1");
tagList[1] = new Tag ("myTag2");
 
 
Add items to cache containing tags:
 
_cache.Add("x-y-z", valueToStore, tagList);
 
 
Get Keys from cache containing tag:
 
ICollection keys = _cache.GetKeysByTag(tagList[0]);
ICollection keys = _cache.GetKeysByAnyTag(tagList);
ICollection keys = _cache.GetKeysByAllTags(tagList);
 
 
Get items from cache containing tag:
 
Hashtable ht = _cache.GetByTag(tagList[0]);
Hashtable ht = _cache.GetByAllTags(tagList);
Hashtable ht = _cache.GetByAnyTag(tagList);
 
 
Remove items from cache containing tag:
 
_cache.RemoveByTag(new Tag("myTag1"));
_cache.RemoveByAllTags(new Tag[] { new Tag("myTag1"), new Tag("myTag2") });
_cache.RemoveByAnyTag(new Tag[] { new Tag("myTag1"), new Tag("myTag2") });
 
 
Using Query string for searching Cache items with respect to Tags:
 
object valueToStore = "Can contain any type";
Tag [] tagList = new Tag [2];
tagList[0] = new Tag ("myTag1");
tagList[1] = new Tag ("myTag2");
 
// Add items to cache containing tags
_cache.Add("x-y-z", valueToStore, tagList);
 
Hashtable queryValue = new Hashtable();
ArrayList queryTagList = new ArrayList();
 
queryTagList.Add("myTag1");
queryTagList.Add("myTag2");
queryValue.Add("$Tag$", queryTagList);
 
// Query string, for searching Cache items with respect to Tags, should be specific to the following
 
_cache.Search("SELECT System.String WHERE this.$Tag$=? AND this.$Tag$=?", queryValue);
 
 
See Also
 
© Copyright  2014 Alachisoft. All rights reserved.