Try Playground
Show / Hide Table of Contents

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

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
  • 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.
  • 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: Cache, Tag, getKeysByTag, getKeysByTags, getByTag, getByTags, getSearchService.
  • 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: Cache, get_by_tag, get_by_tags, get_keys_by_tag, get_keys_by_tags, get_search_service.
  • 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: Cache, Tag, getKeysByTag, getKeysByTags, getByTag, getByTags, getSearchService.
  • 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.Runtime.Caching namespace in your application.

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.

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
// 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");
    }
}
// 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 = null;

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");
    }
} else {
    System.out.println("No keys found for VIP category");
}
# Precondition: Cache is already connected
# Following tags are created and item is added in the cache with these tags
tags = [
    ncache.Tag("Important Customers"),
    ncache.Tag("East Coast Customers")
]

# Keys with the Tag 'Important Customers' are retrieved
# Retrieved Cache keys are then stored in a list
search_service = cache.get_search_service()
retrieved_keys = search_service.get_keys_by_tag(tags[0])

if retrieved_keys is not None and len(retrieved_keys) > 0:
    # Iterate over key result
    for key in retrieved_keys:
        # Perform operations
        print(key)
// Precondition: Cache is already connected
// This is an async method

// Following tags are created and item is added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");

// Keys with the Tag 'Important Customers' are retrieved
// Retrieved Cache keys are then stored in an ICollection
var searchService = await this.cache.getSearchService();
var retrievedKeys = await searchService.getKeysByTags(tags
[0]);

if (retrievedKeys != null && retrievedKeys.size() > 0)
{
    // Iterate over key result
    retrievedKeys.forEach(key => {
        // Perform operations
    });
}
//For finding keys related to one tags
ICollection keys = cache.GetKeysByTag(productTags[0]);
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.

  • .NET
  • Java
  • Python
  • Node.js
// 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'");
    }
}
// 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'
Collection<String> keys = null;
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'");
    }
}
# Following tags are created and item is added in the cache with these tags
tags = [
    ncache.Tag("Important Customers"),
    ncache.Tag("East Coast Customers"),
    ncache.Tag("West Coast Customers")
]

# Keys with the Tag 'Important Customers' are retrieved
# Retrieved Cache keys are then stored in a list
search_service = cache.get_search_service()
retrieved_keys = search_service.get_keys_by_tag(None,
"*Customers")

if retrieved_keys is not None and len(retrieved_keys) > 0:
    # Iterate over key result
    for key in retrieved_keys:
        # Perform operations
        print(key)

# Retrieve all the keys associated with the tags 'East Coast Customers' and 'West Coast Customers'
retrieved_coast_customers_keys = search_service.get_by_tag(None, "??st Customers")

if retrieved_coast_customers_keys is not None and len(retrieved_coast_customers_keys) > 0:
    # Iterate over key result
    for key in retrieved_coast_customers_keys:
        # Perform operations
        print(key)
// This is an async method
// Following tags are created and items are added in the cache with these tags
var tags = new ncache.Tag[3];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");
tags[2] = new ncache.Tag("West Coast Customers");

// Retrieves all the keys 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 retrieveKeys = await searchService.getKeysByTag
("*Customers");

if (retrieveKeys != null && retrieveKeys.Count > 0)
{
    // Iterate over key result
    retrieveKeys.forEach(key => {
        // Perform operations
    });
}

// Retrieves all the keys associated with the tags 'East Coast Customers' and 'West Coast Customers'
var searchService = await this.cache.getSearchService();
var retrieveCoastCustomersKeys = await searchService.getByTags("??st Customers");

if (retrieveCoastCustomersKeys != null && retrieveCoastCustomersKeys.size() > 0)
{
    // Perform operations as done for '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.

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
// 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");
    }
}
// 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
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 = null;
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");
    }
} else {
    System.out.println("No customers found belonging to either East or West Coast");
}
# Following tags are created and items are added in the cache with these tags
tags = [
    ncache.Tag("Important Customers"),
    ncache.Tag("East Coast Customers")
]

# Keys associated with any of the Tag in the list 'tags' are retrieved
# Retrieved Cache keys are then stored in an ICollection
search_service = cache.get_search_service()
keys = search_service.get_by_tags(tags, ncache.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:
        # Perform operations
        print(key)
// This is an async method

// Following tags are created and items are added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");

// Keys associated with any of the Tag in the list 'tags' are retrieved
// Retrieved Cache keys are then stored in an ICollection
var searchService = await this.cache.getSearchService();
var keys = await searchService.getByTags(tags, ncache.
TagSearchOptions.ByAnyTag);

// Check if any keys are failed to retrieve
if (keys != null && keys.size() > 0)
{
    // Iterate over key result
    keys.forEach(key => {
        // Perform operations
    });
}
//For Finding keys containing at least any one tag from tag list.
ICollection keys = cache.GetKeysByAnyTag(productTags);

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.

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
// 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");
    }
}
// A user wants to get the cache keys of all the customer belonging to the category of VIP Customers and West Coast Customers

// 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 call the tags in the list are retrieved
Collection<String> keys = null;
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");
    }
} else {
    System.out.println("No VIP customers from West Coast found");
}
# Following tags are created and items are added in the cache with these tags
tags = [
    ncache.Tag("Important Customers"),
    ncache.Tag("East Coast Customers")
]

# Keys associated with any of the Tag in the list 'tags' are retrieved
# Retrieved Cache keys are then stored in an ICollection
search_service = cache.get_search_service()
keys = search_service.get_by_tags(tags, ncache.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:
        # Perform operations
        print(key)
// This is an async method

// Following tags are created and items are added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");

// Keys associated with any of the Tag in the list 'tags' are retrieved
// Retrieved Cache keys are then stored in an ICollection
var searchService = await this.cache.getSearchService();
var keys = await searchService.getByTags(tags, ncache.
TagSearchOptions.ByAllTags);

// Check if any keys are failed to retrieve
if (keys != null && keys.size() > 0)
{
    // Iterate over key result
    keys.forEach(key => {
        // Perform operations
    });
}
//For finding keys containing all tags from tag list.
ICollection keys = cache.GetKeysByAllTags(productTags);

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.

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
// 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");
    }
}
// A User wants to see the cache keys of the VIP Customers stored in the cache

// Create a tag object
String tag = "*IP Customers";
Map<String, Customer> customers = cache.getSearchService().getByTag(tag);

if (customers != null && !customers.isEmpty()) {
    // Iterate over key result
    for (var customer : customers.values()) {
        Customer customer = (Customer) customerObject;
        System.out.println("Customer with key '" + customer.getCustomerID() + "' belongs to VIP category");
    }
} else {
    System.out.println("No keys found for VIP category");
}

# Following tags are created and item is added in the cache with these tags
tags = [
    ncache.Tag("Important Customers"),
    ncache.Tag("East Coast Customers")
]

# Keys with the Tag 'Important Customers' are retrieved
# Retrieved Cache keys are then stored in a list
search_service = cache.get_search_service()
retrieved_items = search_service.get_by_tag(tags[0])

if retrieved_items is not None and len(retrieved_items) > 0:
    # Iterate over the result
    for item in retrieved_items:
        # Perform operations
        print(retrieved_items[item])
// This is an async method

// Following tags are created and item is added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");

// Keys and values with the Tag 'Important Customers' and 'East Coast Customers' are retrieved
var searchService = await this.cache.getSearchService();
var retrievedList = await searchService.getByTags(tags,
ncache.TagSearchOptions.ByAllTags);

if (retrievedList != null)
{
    retrievedList.forEach(keyValuePair => {
        // Perform operations
    });
}
else
{
    // Object not of Customer type
}
//For finding keys and values related to one tag.
Hashtable result = cache.GetByTag(productTags[0]);

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.

  • .NET
  • Java
  • Python
  • Node.js
// 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'");
    }
}
// A User wants to see the cache keys of the VIP Customers stored in the cache

// Create a tag object
String tag = "*IP Customers";
Map<String, Customer> customers = cache.getSearchService().getByTag(tag);
if (customers != null && !customers.isEmpty()) {
    // Iterate over key result
    for (var customer : customers.values()) {
        Customer customer = (Customer) customerObject;
        System.out.println("Customer with key '" + customer.getCustomerID() + "' belongs to VIP category");
    }
} else {
    System.out.println("No keys found for VIP category");
}
# Following tags are created and item is added in the cache with these tags
tags = [
    ncache.Tag("Important Customers"),
    ncache.Tag("East Coast Customers"),
    ncache.Tag("West 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'

search_service = cache.get_search_service()
retrieved_items = search_service.get_by_tag(None,
"*Customers")

if retrieved_items is not None:
    for item in retrieved_items:
        # Perform operations
        print(item)
else:
    # Object not of Customer type
    print("Object not found")

# Retrieve all the data associated with the tags 'East Coast Customers' and 'West Coast Customers'

retrieved_items_ew = search_service.get_by_tag(None, "??st
Customers")

if retrieved_items_ew is not None:
    for item in retrieved_items_ew:
        # Perform operations
        print(item)
else:
    # Object not of Customer type
    print("No object found")
// This is an async method

// Following tags are created and items are added in the cache with these tags
var tags = new ncache.Tag[3];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");
tags[2] = new ncache.Tag("West 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
("*Customers");

if (retrievedList != null)
{
    retrievedList.forEach(keyValuePair => {
        // Perform operations
    });
}
else
{
    // Object not of Customer type
}

// Retrieves all the data associated with the tags 'East Coast Customers' and 'West Coast Customers'

var searchService = await this.cache.getSearchService();
var retrievedListEW = await searchService.getByTag("??st
Customers");

if(retrievedList != null)
{
    retrievedList.forEach(keyValuePair => {
        // Perform similar operations as done for "customers"
    });
}
else
{
    // Object not of Customer type
}

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.

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
// 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");
    }
}
// A User wants to see the cache keys of the VIP Customers stored in the cache

// Create a tag object
Tag[] tags = new Tag[2];
tags[0] = new Tag("East Coast Customers");
tags[1] = new Tag("West Coast Customers");
Map<String, Customer> customers = cache.getByTags(List.of(tags), TagSearchOptions.ByAnyTag);

if (customers != null && !customers.isEmpty()) {
    // Iterate over key result
    for (var customer : customers.values()) {
        Customer customer = (Customer) customerObject;
        System.out.println("Customer with key '" + customer.getCustomerID() + "' belongs to VIP category");
    }
} else {
    System.out.println("No keys found for VIP category");
}
# Following tags are created and item is added in the cache with these tags
tags = [
    ncache.Tag("Important Customers"),
    ncache.Tag("East Coast Customers")
]

# Keys and values with any of the Tags in the tags list are retrieved
search_service = cache.get_search_service()
retrieved_items = search_service.get_by_tags(tags, ncache.
TagSearchOptions.BY_ANY_TAG)

if retrieved_items is not None:
    for item in retrieved_items:
        # Perform operations
        print(item)
else:
    # Object not of Customer type
    print("Object not found")
// This is an async method

// Following tags are created and item is added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");

// Keys and values with any of the Tags in the tags list are retrieved

var searchService = await this.cache.getSearchService();
var retrievedList = await searchService.getByTags(tags,
ncache.TagSearchOptions.ByAnyTag);

if (retrievedList != null)
{
    retrievedList.forEach(keyValuePair => {
        // Perform operations
    });
}
else
{
    // Object not of Customer type
}
    //For finding keys and values containing at least any one tag in the tag list
    Hashtable result = cache.GetByAnyTag(productTags);

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.

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
// 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");
    }
}
// A User wants to see the cache keys of the VIP Customers stored in the cache

// Create a tag object
ArrayList<Tag> tags = new ArrayList<>();
tags.add(new Tag("East Coast Customers"));
tags.add(new Tag("VIP Customers"));

Map<String, Customer> customers = cache.getSearchService().getByTags(tags, TagSearchOptions.ByAllTags);

if (customers != null && !customers.isEmpty()) {
    // Iterate over key result
    for (var customer : customers.values()) {
        Customer customer = (Customer) customerObject;
        System.out.println("Customer with key '" + customer.getCustomerID() + "' belongs to VIP category");
    }
} else {
    System.out.println("No keys found for VIP category");
}
# Following tags are created and item is added in the cache with these tags
tags = [
    ncache.Tag("Important Customers"),
    ncache.Tag("East Coast Customers")
]

# Keys and values with any of the Tags in the tags list are retrieved
search_service = cache.get_search_service()
retrieved_items = search_service.get_by_tags(tags, ncache.TagSearchOptions.BY_ALL_TAGS)

if retrieved_items is not None:
    for item in retrieved_items:
        # Perform operations
        print(item)
else:
    # Object not of Customer type
    print("Object not found")
// This is an async method

// Following tags are created and item is added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");

// Keys and values with any of the Tags in the tags list are retrieved

var searchService = await this.cache.getSearchService();
var retrievedList = await searchService.getByTags(tags, ncache.TagSearchOptions.ByAllTags);

if (retrievedList != null)
{
    retrievedList.forEach(keyValuePair => {
        // Perform operations
    });
}
else
{
    // Object not of Customer type
}
//For finding keys and values related to all tags from the tag list
Hashtable result = cache.GetByAllTags(productTags);

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.

In This Article
  • Prerequisites to Retrieve Cache Data with Tags
  • Retrieve Keys Associated with a Tag
    • Get Keys By Tag
    • Get Keys By Tag with Wildcard expressions
    • Get Keys By Any Tag
    • Get Keys By All Tags
  • Retrieve Cached Data from Cache Containing Tag
    • Get By Tag
  • Get By Tag with Wildcard Expressions
    • Get By Any Tag
    • Get By All Tags
  • Additional Resources
  • See Also

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Professional
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - 2025. All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top