Once data is tagged with one or more Tags in the cache, it can be efficiently retrieved based on business-logic groupings rather than individual cache keys. NCache allows you to fetch cached items or their keys using a single Tag, any matching Tag, or only those entries that contain all specified Tags. This tag-based retrieval supports powerful filtering scenarios, such as customer segmentation, product categorization, or regional data access, and leverages NCache's optimized SearchService API to perform fast, scalable queries without scanning the entire cache, even for large datasets.
Prerequisites
- Install either of the following NuGet packages in your .NET client application:
- Enterprise:
Install-Package Alachisoft.NCache.SDK -Version 4.9.1.0
- Professional:
Install-Package Alachisoft.NCache.Professional.SDK -Version 4.9.1.0
- Create a new Console Application.
- Make sure that the data being added is serializable.
- Add NCache References by locating
%NCHOME%\NCache\bin\assembly\4.0 and adding Alachisoft.NCache.Web and Alachisoft.NCache.Runtime as appropriate.
- Include the
Alachisoft.NCache.Web.Caching and Alachisoft.NCache.Runtime.Caching namespaces in your application.
- To learn more about the NCache Legacy API, please download the NCache 4.9 documents available as a .zip file on the Alachisoft Website.
How to 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 a specified Tag. The specified Tag is the search criterion 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");
// Retrieve 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");
}
}
// 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");
// Retrieve Cache keys for VIP Customers
Collection<String> keys = cache.getSearchService().getKeysByTag(tag);
if (keys != null && !keys.isEmpty())
{
// Iterate over key result
for (String key : keys)
{
System.out.println("Customer with key '" + key + "' belongs to VIP category");
}
}
# 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("VIP Customers")
# Retrieve Cache keys for VIP Customers
search_service = cache.get_search_service()
retrieved_keys = search_service.get_keys_by_tag(tag)
if retrieved_keys is not None and len(retrieved_keys) > 0:
# Iterate over key result
for key in retrieved_keys:
print(f"Customer with key '{key}' belongs to VIP category")
// A user wants to see the cache keys of the VIP Customers stored in the cache
// Precondition: Cache is already connected
// This is an async method
// Create a Tag object
var tag = new ncache.Tag("VIP Customers");
// Retrieve Cache keys for VIP Customers
var searchService = await this.cache.getSearchService();
var retrievedKeys = await searchService.getKeysByTag(tag);
if (retrievedKeys != null && retrievedKeys.length > 0)
{
// Iterate over key result
retrievedKeys.forEach(key => {
console.log(`Customer with key '${key}' belongs to VIP category`);
});
}
// Using NCache Enterprise 4.9.1
// 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");
// Retrieve Cache keys for VIP Customers
ICollection keys = cache.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"
// Precondition: Cache is already connected
// 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 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'");
}
}
// A user wants to see all the keys of the items stored in the cache with Tags having the suffix "Coast Customers".
// Precondition: Cache is already connected
// 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'
Collection<String> keys = cache.getSearchService().getKeysByTag(wildcard);
if (keys != null && !keys.isEmpty())
{
// Iterate over key result
for (String key : keys)
{
System.out.println("Customer with key '" + key + "' has a Tag with suffix 'Coast Customers'");
}
}
# A user wants to see all the keys of the items stored in the cache with Tags having the suffix "Coast Customers"
# Precondition: Cache is already connected
# 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 for Tags
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'
search_service = cache.get_search_service()
retrieved_keys = search_service.get_keys_by_tag(None, wildcard)
if retrieved_keys is not None and len(retrieved_keys) > 0:
# Iterate over key result
for key in retrieved_keys:
print(f"Customer with key '{key}' has a tag with suffix 'Coast Customers'")
// A user wants to see all the keys of the items stored in the cache with Tags having the suffix "Coast Customers"
// Precondition: Cache is already connected
// This is an async method
// 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 for Tags
var 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'
var searchService = await this.cache.getSearchService();
var retrieveKeys = await searchService.getKeysByTag(wildcard);
if (retrieveKeys != null && retrieveKeys.length > 0)
{
// Iterate over key result
retrieveKeys.forEach(key => {
console.log(`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 Tags provided in the list tags.
// A user wants to get keys of the cached items belonging to either 'East Coast' or 'West Coast' regions
// Precondition: Cache is already connected
// 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");
}
}
// A user wants to get keys of the cached items belonging to either 'East Coast' or 'West Coast' regions
// Precondition: Cache is already connected
// Create an array of Tags to search
List<Tag> tags = List.of(
new Tag("East Coast Customers"),
new Tag("West Coast Customers")
);
// Keys associated with any of the Tag in the list 'tags' are retrieved
Collection<String> keys = cache.getSearchService().getKeysByTags(tags, TagSearchOptions.ByAnyTag);
if (keys != null && !keys.isEmpty())
{
// Iterate over key result
for (String key : keys) {
System.out.println("Customer with key '" + key + "' belongs to either East or West Coast");
}
}
# A user wants to get keys of the cached items belonging to either 'East Coast' or 'West Coast' regions
# Precondition: Cache is already connected
# Create an array of Tags to search
tags = [
Tag("East Coast Customers"),
Tag("West Coast Customers")
]
# Keys associated with any of the Tag in the list 'tags' are retrieved
search_service = cache.get_search_service()
keys = search_service.get_keys_by_tags(tags, TagSearchOptions.BY_ANY_TAG)
# Check if any keys failed to be retrieved
if keys is not None and len(keys) > 0:
# Iterate over key result
for key in keys:
print(f"Customer with key '{key}' belongs to either East or West Coast")
// A user wants to get keys of the cached items belonging to either 'East Coast' or 'West Coast' regions
// Precondition: Cache is already connected
// This is an async method
// Create an array of Tags to search
var tags = [
new ncache.Tag("East Coast Customers"),
new ncache.Tag("West Coast Customers")
];
// Keys associated with any of the Tag in the list 'tags' are retrieved
var searchService = await this.cache.getSearchService();
var keys = await searchService.getByTags(tags, ncache.TagSearchOptions.ByAnyTag);
if (keys != null)
{
// Iterate over key result
keys.forEach((value, key) => {
console.log(`Customer with key '${key}' belongs to either East or West Coast`);
});
}
// Using NCache Enterprise 4.9.1
// Precondition: Cache is already connected
// 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 keys = cache.GetKeysByAnyTag(tags);
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");
}
}
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
// Precondition: Cache is already connected
// 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");
}
}
// A user wants to get the cache keys of all the customer belonging to the category of VIP Customers and West Coast Customers
// Precondition: Cache is already connected
// Create an array of Tags to search
List<Tag> tags = List.of(
new Tag("East Coast Customers"),
new Tag("West Coast Customers")
);
// Keys associated with all the Tags in the list are retrieved
Collection<String> keys = cache.getSearchService().getKeysByTags(tags, TagSearchOptions.ByAllTags);
if (keys != null && !keys.isEmpty())
{
// Iterate over key result
for (String key : keys)
{
System.out.println("Customer with key '" + key + "' is a VIP customer from West Coast");
}
}
# A user wants to get the cache keys of all the customer belonging to category of VIP Customers and West Coast Customers
# Precondition: Cache is already connected
# Create an array of Tags to search
tags = [
Tag("VIP Customers"),
Tag("West Coast Customers")
]
# Keys associated with any of the Tag in the list 'tags' are retrieved
search_service = cache.get_search_service()
keys = search_service.get_keys_by_tags(tags, TagSearchOptions.BY_ALL_TAGS)
# Check if any keys failed to be retrieved
if keys is not None and len(keys) > 0:
# Iterate over key result
for key in keys:
print(f"Customer with key '{key}' is a VIP customer from West Coast")
// A user wants to get the cache keys of all the customer belonging to category of VIP Customers and West Coast Customers
// Precondition: Cache is already connected
// This is an async method
// Create an array of Tags to search
var tags = [
new ncache.Tag("VIP Customers"),
new ncache.Tag("West Coast Customers")
];
// Keys associated with all of the Tags in the list are retrieved
var searchService = await this.cache.getSearchService();
var keys = await searchService.getByTags(tags, ncache.TagSearchOptions.ByAllTags);
if (keys != null )
{
// Iterate over key result
keys.forEach((value, key) => {
console.log(`Customer with key '${key}' is a VIP customer from West Coast`);
});
}
// Using NCache Enterprise 4.9.1
// A user wants to get the cache keys of all the customer belonging to category of VIP Customers and West Coast Customers
// Precondition: Cache is already connected
// 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 keys = cache.GetKeysByAllTags(tags);
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");
}
}
How to 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
// Precondition: Cache is already connected
// Create a Tag object
Tag tag = new Tag("VIP Customers");
// Retrieve Cache keys for VIP Customers
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");
}
}
// 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
String tag = "VIP Customers";
// Retrieve Cache keys for VIP Customers
Map<String, Customer> retrievedMap = cache.getSearchService().getByTag(tag);
if (retrievedMap != null)
{
// Iterate over key result
for (Customer customer : retrievedMap.values())
{
System.out.println("Customer " + customer.getContactName() + "having ID " + customer.getCustomerID() + " belongs to VIP category");
}
}
# A user wants to see the details of the VIP Customers stored in the cache
# Precondition: Cache is already connected
# Create a Tag object
tag = Tag("VIP Customers")
# Retrieve Cache keys for VIP Customers
search_service = cache.get_search_service()
retrieved_items = search_service.get_by_tag(tag)
if retrieved_items is not None and len(retrieved_items) > 0:
# Iterate over the result
for key, value in retrieved_items.items():
name = value.get('name')
customer_id = value.get('customer_id')
print(f"Customer {name} having ID '{customer_id}' belongs to VIP category")
// A user wants to see the details of the VIP Customers stored in the cache
// This is an async method
// Precondition: Cache is already connected
// Create a Tag object
var tag = "VIP Customers";
// Retrieve Cache keys for VIP Customers
var searchService = await this.cache.getSearchService();
var retrievedList = await searchService.getByTag(tag);
if (retrievedList != null)
{
retrievedList.forEach((value,key) => {
console.log(`Customer with key '${key}' belongs to VIP category`);
});
}
// Using NCache Enterprise 4.9.1
// A user wants to see the details of the VIP Customers stored in the cache
// Precondition: Cache is already connected
// Create a Tag object
Tag tag = new Tag("VIP Customers");
// Retrieve Cache keys for VIP Customers
Hashtable result = cache.GetByTag(tag);
if (result != null)
{
foreach (DictionaryEntry entry in result)
{
if (entry.Value is Customer customer)
{
Console.WriteLine($"Customer {customer.CompanyName} having ID '{customer.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:
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"
// Precondition: Cache is already connected
// 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'");
}
}
// 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"
// Precondition: Cache is already connected
// 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'
Map<String, Customer> retrievedMap = cache.getSearchService().getByTag(wildcard);
if (retrievedMap != null)
{
// Iterate over key result
for (Customer customer : retrievedMap.values())
{
System.out.println("Customer " + customer.getContactName() + "having ID " + customer.getCustomerID() + " has a tag with suffix 'Coast Customers'");
}
}
# 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"
# Precondition: Cache is already connected
# Create a wildcard string to for Tags
wildcard = "*Coast Customers"
# Retrieves items which contain a Tag with suffix 'Customers'
# This includes 'VIP Customers' , 'East Coast Customers', and 'West Coast Customers'
search_service = cache.get_search_service()
retrieved_items = search_service.get_by_tag(None, wildcard)
if retrieved_items is not None:
for key, value in retrieved_items.items():
name = value.get('name')
customer_id = value.get('customer_id')
print(f"Customer {name} having ID '{customer_id}' has a tag with suffix 'Coast Customers'")
// 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"
// Precondition: Cache is already connected
// This is an async method
// Create a wildcard string to for Tags
var wildcard = "*Coast Customers";
// Retrieves all the data associated with customers containing any prefix before customer, i.e., 'Important Customers' as well as 'East Coast Customers' and 'West Coast Customers'
var searchService = await this.cache.getSearchService();
var retrievedList = await searchService.getByTag(wildcard);
if (retrievedList != null)
{
retrievedList.forEach((value,key) => {
console.log(`Customer with key '${key}' 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
// Precondition: Cache is already connected
// 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");
}
}
// A user wants to see the details of the customers belonging to either East Coast or West Coast
// Precondition: Cache is already connected
// 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
Map<String, Customer> retrievedMap = cache.getSearchService().getByTags(List.of(tags), TagSearchOptions.ByAnyTag);
if (retrievedMap != null)
{
// Iterate over key result
for (Customer customer : retrievedMap.values())
{
System.out.println("Customer " + customer.getContactName() + "having ID " + customer.getCustomerID() + " belongs to either East or West Coast");
}
}
# A user wants to see the details of the customers belonging to either East Coast or West Coast
# Precondition: Cache is already connected
# Create an array of Tags to search
tags = [
Tag("East Coast Customers"),
Tag("West Coast Customers")
]
# Keys and values associated with any of the Tag in the list 'tags' are retrieved
search_service = cache.get_search_service()
retrieved_items = search_service.get_by_tags(tags, TagSearchOptions.BY_ANY_TAG)
if retrieved_items is not None:
for key, value in retrieved_items.items():
name = value.get('name')
customer_id = value.get('customer_id')
print(f"Customer {name} having ID '{customer_id}' belongs to either East or West Coast")
// A user wants to see the details of the customers belonging to either East Coast or West Coast
// Precondition: Cache is already connected
// This is an async method
// Create an array of Tags to search
var tags = [
new ncache.Tag("East Coast Customers"),
new ncache.Tag("West Coast Customers")
];
// Keys and values associated with any of the Tag in the list 'tags' are retrieved
var searchService = await this.cache.getSearchService();
var retrievedList = await searchService.getByTags(tags,ncache.TagSearchOptions.ByAnyTag);
if (retrievedList != null)
{
retrievedList.forEach((value,key) => {
console.log(`Customer with key '${key}' belongs to either East or West Coast`);
});
}
// Using NCache Enterprise 4.9.1
// A user wants to see the details of the customers belonging to either East Coast or West Coast
// Precondition: Cache is already connected
// 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
Hashtable result = cache.GetByAnyTag(tags);
if (result != null)
{
foreach (DictionaryEntry entry in result)
{
if (entry.Value is Customer customer)
{
Console.WriteLine($"Customer {customer.CompanyName} having ID '{customer.CustomerID}' belongs to either East or West Coast");
}
}
}
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
// Precondition: Cache is already connected
// 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");
}
}
// A user wants to see the details of all the customers belonging to the category of the VIP Customers and the West Coast Customers
// Precondition: Cache is already connected
// Create an array of Tags to search
ArrayList<Tag> tags = new ArrayList<>();
tags.add(new Tag("VIP Customers"));
tags.add(new Tag("West Coast Customers"));
// Keys and values associated with all of the Tags in the list 'tags' are retrieved
Map<String, Customer> retrievedMap = cache.getSearchService().getByTags(tags, TagSearchOptions.ByAllTags);
if (retrievedMap != null)
{
for (Customer customer : retrievedMap.values())
{
System.out.println("Customer " + customer.getContactName() + "having ID " + customer.getCustomerID() + " is a VIP customer from West Coast");
}
}
# A user wants to see the details of all the customers belonging to the category of the VIP Customers and the West Coast Customers
# Precondition: Cache is already connected
# Create an array of Tags to search
tags = [
Tag("VIP Customers"),
Tag("West Coast Customers")
]
# Keys and values associated with all of the Tags in the list 'tags' are retrieved
search_service = cache.get_search_service()
retrieved_items = search_service.get_by_tags(tags, TagSearchOptions.BY_ALL_TAGS)
if retrieved_items is not None:
for key, value in retrieved_items.items():
name = value.get('name')
customer_id = value.get('customer_id')
print(f"Customer {name} having ID '{customer_id}' is a VIP customer from West Coast")
// A user wants to see the details of all the customers belonging to the category of the VIP Customers and the West Coast Customers
// Precondition: Cache is already connected
// This is an async method
// Create an array of Tags to search
var tags = [
new ncache.Tag("VIP Customers"),
new ncache.Tag("West Coast Customers")
];
// Keys and values associated with all of the Tags in the list 'tags' are retrieved
var searchService = await this.cache.getSearchService();
var retrievedList = await searchService.getByTags(tags, ncache.TagSearchOptions.ByAllTags);
if (retrievedList != null)
{
retrievedList.forEach((value,key) => {
console.log(`Customer with key '${key}' is a VIP customer from West Coast`);
});
}
// Using NCache Enterprise 4.9.1
// A user wants to see the details of all the customers belonging to the category of the VIP Customers and the West Coast Customers
// Precondition: Cache is already connected
// 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
Hashtable result = cache.GetByAllTags(tags);
if (result != null)
{
foreach (DictionaryEntry entry in result)
{
if (entry.Value is Customer customer)
{
Console.WriteLine($"Customer {customer.CompanyName} having ID '{customer.CustomerID}' is a VIP customer from West Coast");
}
}
}
Additional Resources
NCache provides a 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.