Retrieve Cache Data with Tags
Once the data is tagged with one or multiple tags in cache data, it can be retrieved using these tags- specified tags, one, or all tags.
Prerequisites to Retrieve Cache Data with Tags
- To learn about the standard prerequisites required to work with all NCache client-side features, please refer to the given page on Client-Side API Prerequisites.
- For API details, refer to: ICache, Tag, GetKeysByTag, GetKeysByTags, GetByTag, GetByTags, SearchService.
Retrieve Keys Associated with a Tag
Keys can be retrieved from the cache data by tags either by using GetKeysByTag
(with wildcard expression) or GetKeysByTags
(with TagSearchOptions
overload). All these methods are explained below.
Get Keys By Tag
GetKeysByTag
is a method that retrieves the keys by one specified tag. The specified tag is the search criteria for the data in the cache.
The following example retrieves the keys of the items associated with the specified tag from the cache.
Warning
Providing a Null
tag array will throw an ArgumentNullException or NullPointerException.
// A User wants to see the cache keys of the VIP Customers stored in the cache
// Precondition: Cache is already connected
// Create a tag object
Tag tag = new Tag("VIP Customers");
// Retrieved Cache keys for VIP Customers
ICollection<string> keys = cache.SearchService.GetKeysByTag(tag);
if (keys != null && keys.Count > 0)
{
// Iterate over key result
foreach (var key in keys)
{
Console.WriteLine($"Customer with key '{key}' belongs to VIP category");
}
}
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Get Keys By Tag with Wildcard expressions
NCache also provides support for retrieving keys from the cache data using tags with wildcard expressions using the method GetKeysByTag(Wildcard). The special characters supported in wild search by NCache are:
*
: Used as a substitute for zero or more characters in the string.?
: Used as a substitute for a single character in the string.
The following example retrieves the keys of the items associated with tags along with wildcard expression.
// A User wants to see all the keys of the items stored in the cache with tags having the suffix "Coast Customers"
// You can also use '?' symbol as a single character substitute.
// For eg., wildcard "??st Customers" can be used to search for the East and West Coast Customers
// Create a wildcard string for tags
string wildcard = "*Coast Customers";
// Retrieves keys of cached items which contain a tag with the suffix '*Coast Customers'.
// This brings 'East Coast Customers' and 'West Coast Customers'
ICollection<string> keys = cache.SearchService.GetKeysByTag(wildcard);
if (keys != null && keys.Count > 0)
{
// Iterate over key result
foreach (var key in keys)
{
Console.WriteLine($"Customer with key '{key}' has a tag with suffix 'Coast Customers'");
}
}
Get Keys By Any Tag
GetKeysByTags
is a method with the search option as ByAnyTag
where multiple tags are provided and the key(s) matching ANY of the provided tags will be retrieved.
The following example retrieves the keys that match ANY of the tag provided in the list tags
.
// A user wants to get keys of the cached items belonging to either 'East Coast' or 'West Coast' regions
// Create an array of tags to search
Tag[] tags = new Tag[2];
tags[0] = new Tag("East Coast Customers");
tags[1] = new Tag("West Coast Customers");
// Keys associated with any of the Tag in the list 'tags' are retrieved
ICollection<string> keys = cache.SearchService.GetKeysByTags(tags,TagSearchOptions.ByAnyTag);
if (keys != null && keys.Count > 0)
{
// Iterate over key result
foreach (var key in keys)
{
Console.WriteLine($"Customer with key '{key}' belongs to either East or West Coast");
}
}
Get Keys By All Tags
GetKeysByTags
is a method with the search option as ByAllTags
where multiple tags are provided and key(s) matching ALL of the provided tags will be retrieved.
The following example retrieves the keys of items in the cache containing ALL the tags in the list tags
.
// A user wants to get the cache keys of all the customer belonging to category of VIP Customers and West Coast Customers
// Create an array of tags to search
Tag[] tags = new Tag[2];
tags[0] = new Tag("VIP Customers");
tags[1] = new Tag("West Coast Customers");
// Keys associated with all of the tags in the list are retrieved
ICollection<string> keys = cache.SearchService.GetKeysByTags(tags, TagSearchOptions.ByAllTags);
if (keys != null && keys.Count > 0)
{
// Iterate over key result
foreach (var key in keys)
{
Console.WriteLine($"Customer with key '{key}' is a VIP customer from West Coast");
}
}
Retrieve Cached Data from Cache Containing Tag
Similar to keys, cached items (keys and values) can also be retrieved from the cache either by using GetByTag
or GetByTags
(having TagSearchOptions
as ByAllTags
and ByAnyTag
). All these methods are explained below.
Get By Tag
GetByTag
is a method in which a single tag is provided and all the keys and values associated with that tag are retrieved.
The following example retrieves the keys, associated with the tag VIP Customers.
// A user wants to see the details of the VIP Customers stored in the cache
// Create a tag object
Tag tag = new Tag("VIP Customers");
// Retrieved Cache keys for VIP Custoemrs
IDictionary<string, Customer> retrievedDictionary = cache.SearchService.GetByTag<Customer>(tag);
if (retrievedDictionary != null)
{
foreach (KeyValuePair<string, Customer> retrievedItem in retrievedDictionary)
{
Console.WriteLine($"Customer {retrievedItem.Value.ContactName} having ID '{retrievedItem.Value.CustomerID}' belongs to VIP category");
}
}
Get By Tag with Wildcard Expressions
NCache also provides support for retrieving keys from the cache using tags with wildcard expressions using the method GetByTag(Wildcard). The special characters supported in the wildcard search by NCache are:
*
: Used as a substitute for zero or more characters in the string.?
: Used as a substitute for a single character in the string.
The following example retrieves the data of the items associated with tags along with wildcard expressions.
// A User wants to see details of the customers belonging to any tag having suffix "Coast Customers".
// For example, "East Coast Customers".
// You can also use '?' symbol as a single character substitute.
// For example, wildcard "??st Customers" can be used to search for the "East and West Coast Customers"
// Create a wildcard string to for tags
string wildcard = "*Coast Customers";
// Retrieves items which contain a tag with suffix 'Customers'
// This includes 'VIP Customers' , 'East Coast Customers', and 'West Coast Customers'
IDictionary<string, Customer> retrievedDictionary = cache.SearchService.GetByTag<Customer>(wildcard);
if (retrievedDictionary != null)
{
foreach (KeyValuePair<string, Customer> retrievedItem in retrievedDictionary)
{
Console.WriteLine($"Customer {retrievedItem.Value.ContactName} having ID '{retrievedItem.Value.CustomerID}' has a tag with suffix 'Coast Customers'");
}
}
Get By Any Tag
GetByTags
is a method and ByAnyTag
is the search option in which a list of tags is provided and all the keys and values associated with ANY of the tags in the tag list are retrieved.
The following example retrieves the keys and values against these keys, associated with any of the tags in the list tags
.
// A user wants to see the details of the customers belonging to either East Coast or West Coast
// Create an array of tags to search
Tag[] tags = new Tag[2];
tags[0] = new Tag("East Coast Customers");
tags[1] = new Tag("West Coast Customers");
// Keys and values associated with any of the Tag in the list 'tags' are retrieved
IDictionary<string, Customer> retrievedDictionary = cache.SearchService.GetByTags<Customer>(tags, TagSearchOptions.ByAnyTag);
if (retrievedDictionary != null)
{
foreach (KeyValuePair<string, Customer> retrievedItem in retrievedDictionary)
{
Console.WriteLine($"Customer '{retrievedItem.Value.ContactName}' having ID '{retrievedItem.Value.CustomerID}' belongs to either East or West Coast");
}
}
Get By All Tags
GetByTags
is a method, and ByAllTags
is the search option in which a list of tags is provided and all the keys and values associated with ALL of the tags in the tag list are retrieved.
The following example retrieves the keys and values against these keys, associated with all of the tags in the list tags
.
// A user wants to see the details of all the customers belonging to the category of the VIP Customers and the West Coast Customers
// Create an array of tags to search
Tag[] tags = new Tag[2];
tags[0] = new Tag("VIP Customers");
tags[1] = new Tag("West Coast Customers");
// Keys and values associated with all of the tags in the list 'tags' are retrieved
IDictionary<string, Customer> retrievedDictionary = cache.SearchService.GetByTags<Customer>(tags, TagSearchOptions.ByAllTags);
if (retrievedDictionary != null)
{
foreach (KeyValuePair<string, Customer> retrievedItem in retrievedDictionary)
{
Console.WriteLine($"Customer '{retrievedItem.Value.ContactName}' having ID '{retrievedItem.Value.CustomerID}' is a VIP customer from West Coast");
}
}
Additional Resources
NCache provides sample application for Tags on GitHub.
See Also
.NET: Alachisoft.NCache.Runtime.Caching namespace.
Java: com.alachisoft.ncache.runtime.caching namespace.
Python: ncache.runtime.caching class.
Node.js: Tag class.