• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Try Free
Show / Hide Table of Contents

Retrieve Cache Data with Tags

Note

This feature is only available in NCache Enterprise Edition.

Once the data is tagged with one or multiple tags, it can be retrieved using these tags- specified tag or one or all tags.

Prerequisites

  • .NET/.NET Core
  • Java
  • Scala
  • Node.js
  • Python
  • 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, 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.

Retrieve Keys Associated with a Tag

Keys can be retrieved from the cache 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 which retrieves the keys by one specified tag. The specified tag is the search criteria for the data in the cache.

Following example retrieves the keys of the items associated with the specified tag from the cache.

Warning

Providing Null tag array will throw an ArgumentNullException or NullPointerException.

  • .NET/.NET Core
  • Java
  • Scala
  • Node.js
  • Python
try
{   
    // Pre-condition: Cache is already connected
    // A User wants to see the cache keys of the VIP Customers stored in the cache

    // 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");
        }
    }
}
catch (OperationFailedException ex)
{
    // Exception can occur due to:
    // Connection Failures 
    // Operation Timeout
    // Operation performed during state transfer
}
catch (Exception ex)
{
    // Any other generic exception like ArgumentNullException or ArgumentException
}
try
{
    // Following tags are created and item is added in the cache with these tags
    Tag[] tags = new Tag[2];
    tags[0] = new Tag("Important Customers");
    tags[1] = new Tag("East Coast Customers");

    // Keys with the Tag 'Important Customers' are retrieved
    // Retrieved Cache keys are then stored in an ICollection
    Collection retrievedKeys = cache.getKeysByTag(tags[0]);

    // Iterate over key result
    if (retrievedKeys != null)
    {
        for (Object keys : retrievedKeys) 
        { 
            // Perform operations        
        }
    }
}
catch (Exception ex) 
{
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
    // Any generic exception
}
try {
    // Following tags are created and item is added in the cache with these tags

    var tags: List[Tag] = List()
    tags = Tag("Important Customers") :: tags
    tags = Tag("East Coast Customers") :: tags

    // Keys with the Tag 'Important Customers' are retrieved
    // Retrieved Cache keys are then stored in a Collection

    val retrievedKeys = cache.getSearchService.getKeysByTag
    (tags.head)

    // Iterate over key result
    if (retrievedKeys != null) {
      for (keys <- retrievedKeys) {
        // Perform operations
      }
    }
}
catch {
    case exception: Exception => {
      // Handle any errors
    }
}
// This is an async method
try
{
    // 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
        });
    }
}
catch(error)
{
    // Handle errors
}
try:
    # 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)
except Exception as exp:
    # Handle errors
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 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 wild card expression.

  • .NET/.NET Core
  • Java
  • Scala
  • Node.js
  • Python

    // 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'");
        }
    }

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

    if (retrieveKeys != null )
    {
        // Iterate over key result
        foreach (var key : retrieveKeys)
        {
            // Perform operations                      
        }
    }

    // Retrieves all the keys associated with the tags
    // 'East Coast Customers' and 'West Coast Customers'
    ICollection<string> retrieveCoastCustomersKeys = cache.
    getKeysByTag("??st Customers");

    if (retrieveCoastCustomersKeys != null)
    {
        // Perform operations as done for 'Customers'
    }

    // Pre-condition: Cache is already connected

    // Following tags are created and items are added in the cache with these tags
    var tags: List[Tag] = List()
    tags = Tag("Important Customers") :: tags
    tags = Tag("East Coast Customers") :: tags
    tags = Tag("West Coast Customers") :: tags

    // 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 retrievedKeys = cache.getSearchService.getKeysByTag
    ("*Customers");

    if (retrievedKeys != null )
    {
      // Iterate over key result
      for (key <- retrievedKeys)
      {
        // Perform operations
      }
    }

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

    if (retrieveCoastCustomersKeys != null)
    {
      // Perform operations as done for 'Customers'
    }
    // This is an async method

    // Pre-condition: Cache is already connected

    // 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'
    }

    # 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)

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.

Following example retrieves the keys which matches ANY of the tag provided in the list tags.

  • .NET/.NET Core
  • Java
  • Scala
  • Node.js
  • Python

    // 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");
        }
    }

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

    // Keys associated with any of the Tag in the list 'tags' are retrieved
    // Retrieved Cache keys are then stored in a Collection
    Collection retrievedKeys = cache.getKeysByTags(productTags
    [0]);

    // Iterate over key result
    if (retrievedKeys != null)
    {
        for (Object key : retrievedKeys) 
        {
            // Perform operations        
        }
    }

    // Following tags are created and items are added in the cache with these tags

    var tags: List[Tag] = List()
    tags = Tag("Important Customers") :: tags
    tags = Tag("East Coast Customers") :: tags

    // Keys associated with any of the Tag in the list 'tags' are retrieved
    // Retrieved Cache keys are then stored in a Collection
    val retrievedKeys = cache.getSearchService.getByTags(tags, 
    TagSearchOptions.ByAnyTag)

    // Iterate over key result
    if (retrievedKeys != null) {
      for (key <- retrievedKeys) {
        // Perform operations
      }
    }
    // This is an async method

    // Pre-condition: Cache is already connected

    // 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
        });
    }

    # Pre-condition: Cache is already connected

    # 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)

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/.NET Core
  • Java
  • Scala
  • Node.js
  • Python

    // 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");
        }
    }

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

    // Keys associated with all of the Tags in the list 'tags' are retrieved
    // Retrieved Cache keys are then stored in an ICollection
    Collection retrievedKeys = cache.getKeysByAllTags(tags[0]);

    // Iterate over key result
    if (retrievedKeys != null)
    {
        for (Object key : retrievedKeys) 
        {
            // Perform operations        
        }
    }

    // Following tags are created and items are added in the cache with these tags

    var tags: List[Tag] = List()
    tags = Tag("Important Customers") :: tags
    tags = Tag("East Coast Customers") :: tags

    // Keys associated with all of the Tag in the list 'tags' are retrieved
    // Retrieved Cache keys are then stored in a Collection
    val retrievedKeys = cache.getSearchService.getByTags(tags, 
    TagSearchOptions.ByAllTags)

    // Iterate over key result
    if (retrievedKeys != null) {
      for (key <- retrievedKeys) {
        // Perform operations
      }
    }
    // This is an async method

    // Pre-condition: Cache is already connected

    // 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
        });
    }

    # Pre-condition: Cache is already connected

    # 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)

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.

Following example retrieves the keys, associated with the tag VIP Customers.

  • .NET/.NET Core
  • Java
  • Scala
  • Node.js
  • Python

     // 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");
        }
    }

    // Pre-condition: Cache is already connected

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

    // Keys and values with the Tag 'Important Customers' and 'East Coast Customers' are retrieved 
    HashMap result = cache.getByTag(tags[0]);

    // Iterate over result
    if (result != null)
    {
        for (KeyValuePair<string, Customer> keyValuePair : result)
        {
            // Perform operations
        }
    }

    // Pre-condition: Cache is already connected

    // Following tags are created and item is added in the cache with these tags
    var tags: List[Tag] = List()
    tags = Tag("Important Customers") :: tags
    tags = Tag("East Coast Customers") :: tags

    // Keys and values with the Tag 'Important Customers' and 'East Coast Customers' are retrieved
    val result = cache.getSearchService.getByTag(tags.head)

    // Iterate over result
    if (result != null) {
      for (keyValuePair <- result) {
        // Perform operations
      }
    }
    // This is an async method

    // Pre-condition: Cache is already connected

    // 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
    }

    # 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])

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 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 data of the items associated with tags along with wild card expressions.

  • .NET/.NET Core
  • Java
  • Scala
  • Node.js
  • Python

    // A User wants to see details of the customers belonging to any tag having suffix "Coast Customers" for eg. East 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 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'");
        }
    }

    // Pre-condition: Cache is already connected
    // Following tags are created and items are added in the cache with these tags
    Tag[] tags = new Tag[3];
    tags[0] = new Tag("Important Customers");
    tags[1] = new Tag("East Coast Customers");
    tags[2] = new 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'
    java.util.Map<String, Customer> retrievedDictionary = cache.
    getSearchService()<Customer>getByTag("*Customers");
    Collection<String> keys = cache.getSearchService().
    <Customer>getKeysByTag("*Customers");

    if (keys != null)
    {
        if(keys.size()>0)
        {
            retrievedDictionary = cache.removeBulk(keys,null);
        }
        for (java.util.Map.Entry<String, Customer>  customer: retrievedDictionary.entrySet())
        {
            //perform operations
        }
    }
    else
    {
        // Object not of Customer type
    }

    // Retrieves all the data associated with the tags
    // 'East Coast Customers' and 'West Coast Customers'
    java.util.Map<String, Customer> retrievedDictionaryEW = 
    cache.getSearchService().<Customer>getByTag("??st Customers");

    if (retrievedDictionary != null)
    {
        for (java.util.Map.Entry<String, Customer> customer : 
        retrievedDictionaryEW.entrySet())
        {
            //perform similar operations as done for "customers"
        }
    }
    else
    {
        // Object not of Customer type
    }

    // Pre-condition: Cache is already connected

    // Following tags are created and items are added in the cache with these tags
    var tags: List[Tag] = List()
    tags = Tag("Important Customers") :: tags
    tags = Tag("East Coast Customers") :: tags
    tags = Tag("West Coast Customers") :: tags

    // 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 retrievedDictionary = cache.getSearchService.getByTag
    ("*Customers")

    if (retrievedDictionary != null) {
      for (customer <- retrievedDictionary) {
        //perform operations
      }
    }
    else {
      // Items not found
    }

    // Retrieves all the data associated with the tags
    // 'East Coast Customers' and 'West Coast Customers'
    val retrievedDictionaryEW = cache.getSearchService.getByTag
    ("??st Customers")

    if (retrievedDictionary != null) {
      for (customer <- retrievedDictionaryEW) {
        //perform similar operations
      }
    }
    else {
    }
    // This is an async method

    // Pre-condition: Cache is already connected

    // 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
    }

    # 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")

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.

Following example retrieves the keys and values against these keys, associated with any of the tags in the list tags.

  • .NET/.NET Core
  • Java
  • Scala
  • Node.js
  • Python
    // 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");
        }
    }

    // Pre-condition: Cache is already connected

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

    // Keys and values with any of the Tags in the tags list are retrieved
    HashMap result = cache.getByAnyTag(tags);

    // Iterate over result
    if (result != null)
    {
        // Perform operations
    }

    // Pre-condition: Cache is already connected

    // Following tags are created and item is added in the cache with these tags
    var tags: List[Tag] = List()
    tags = Tag("Important Customers") :: tags
    tags = Tag("East Coast Customers") :: tags

    // Keys and values with any of the Tags in the tags list are retrieved
    val result = cache.getSearchService.getByTags(tags, TagSearchOptions.ByAnyTag)

    // Iterate over result
    if (result != null) {
      // Perform operations
    }
    // This is an async method
    // Pre-condition: Cache is already connected
    // 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
    }

    # Pre-condition: 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 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")

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.

Following example retrieves the keys and values against these keys, associated with all of the tags in the list tags.

  • .NET/.NET Core
  • Java
  • Scala
  • Node.js
  • Python

    // 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");
        }
    }

    // Pre-condition: Cache is already connected

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

    // Keys and values with all of the Tags in the tags list are retrieved
    HashMap result = cache.getByAllTags(tags);

    // Iterate over result
    if (result != null)
    {
        // Perform operations
    }

    // Pre-condition: Cache is already connected

    // Following tags are created and item is added in the cache with these tags
    var tags: List[Tag] = List()
    tags = Tag("Important Customers") :: tags
    tags = Tag("East Coast Customers") :: tags

    // Keys and values with any of the Tags in the tags list are retrieved
    val result = cache.getSearchService.getByTags(tags, TagSearchOptions.ByAllTags)

    // Iterate over result
    if (result != null) {
      // Perform operations
    }
    // This is an async method
    // Pre-condition: Cache is already connected
    // 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
    }

    # Pre-condition: 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 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")

Additional Resources

NCache provides sample application for Tags on GitHub.

See Also

Use Groups for Logical Data Grouping
Add/Update Cache Data with Tags
Remove Cache Data with Tags
Search Tag Data in Cache with SQL
Delete Tag Data from Cache with SQL
Named Tags with Cache Data

Back to top Copyright © 2017 Alachisoft