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

Retrieve Cache Data with Groups

Note

This feature is only available in NCache Enterprise Edition.

Once the items are added to the specific groups, the user can also retrieve certain cache items or keys associated with that group.

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, GetGroupKeys, GetGroupData, 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, getGroupKeys, getGroupData, 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, getGroupKeys, getGroupData, 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_group_keys, get_group_data, get_search_service.

Retrieve Keys of a Particular Group

To return a list of keys that belongs to a specified group, you can use the GetGroupKeys method that returns a list of keys mapped under the group passed to this API.

Important
  • Passing empty strings for the group will return an empty result set.
  • Passing only the value of the group will return all the keys mapped under the group.
  • Passing null to this API for the group will throw an ArgumentNullException.
  • .NET/.NET Core
  • Java
  • Scala
  • Node.js
  • Python
try
{   
    // Pre-conditions: Cache is connected

    //A user wants to get the cache keys of all the customers from the West Coast

    string groupName = "West Coast Customers";

    ICollection<string> keys = cache.SearchService.GetGroupKeys(groupName);

    if (keys != null && keys.Count > 0)
    {
        // Iterate over the result
        foreach (var key in keys)
        {
            Console.WriteLine($"Key '{key}' belongs to '{groupName}' group.");     
        }
    }
}
catch (OperationFailedException ex)
{
    // Exception can occur due to:
    // Connection Failures 
    // Operation Timeout
    // Operation performed during state transfer
}
catch (Exception ex)
{
    // Any generic exception like ArgumentNullException or ArgumentException
}
try
{
    // Data with this group already exists in cache
    String groupName = "Important Customers";

    java.util.Collection<String> retrievedKeys = cache.getSearchService().getGroupKeys(groupName);

    if (retrievedKeys != null && retrievedKeys.size() > 0)
    {
        // Iterate over the result
        for (String key : retrievedKeys)
        {
            // Perform operations
        }
    }
    else
    {
        // No data against the group found
    }
}
catch (OperationFailedException ex)
{
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
}
catch (Exception ex)
{
    // Any generic exception like NullPointerException or IllegalArgumentException
}
try {
    // Data with this group already exists in cache

    val groupName = "Important Customers"

    val retrievedKeys = cache.getSearchService.getGroupKeys(groupName)

    if (retrievedKeys != null && retrievedKeys.nonEmpty) {
      // Iterate over the result
      for (key <- retrievedKeys) {
        // Perform operations
      }
    }
    else {
      // No data against the group found
    }
}
catch {
    case exception: Exception => {
      // Handle any errors
    }
}
// This is an async method
try
{
    // Data with this group already exists in cache
    var groupName = "Important Customers";

    var searchService = await this.cache.getSearchService();
    var retrievedKeys = await searchService.getGroupKeys(groupName);

    if (retrievedKeys != null && retrievedKeys.size() > 0)
    {
        // Iterate over the result
        retrievedKeys.forEach(key => {
            // Perform Operations
        });
    }
    else
    {
        // No data against the group found
    }
}
catch(error)
{
    // Handle errors
}
try:
    # Data with this group already exists in cache
    group_name = "Important Customers"

    search_service = cache.get_search_service()
    retrieved_keys = search_service.get_group_keys(group_name)

    if retrieved_keys is not None and len(retrieved_keys) > 0:
        # Iterate over the result
        for key in retrieved_keys:
            # Perform Operations
            print(key)
    else:
        # No data against the group found
        print("No keys found")
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.

Retrieve Keys and Values of a Particular Group

To return the dictionary of the keys and the values that belong to a specified group, the GetGroupData method can be used. This method returns a dictionary of keys and values mapped under the group passed to this API.

The following example retrieves the keys and the values that belong to the group West Coast Customers.

  • .NET/.NET Core
  • Java
  • Scala
  • Node.js
  • Python
/* A user wants to get the cache keys along with the values of all the customers from the West Coast*/

string groupName = "West Coast Customers";

IDictionary<string, Customer> retrievedDictionary = cache.SearchService.GetGroupData<Customer>(groupName);

if (retrievedDictionary != null)
{
    foreach (KeyValuePair<string, Customer> retrievedItem in retrievedDictionary)
    {
        Console.WriteLine($"Customer '{retrievedItem.Value.ContactName}' having Key '{retrievedItem.Key}' belongs to West Coast");
    }
}

    // Data with this group already exists in cache
    String groupName = "Important Customers";

    java.util.Map<String, Customer> result = cache.getSearchService().<Customer>getGroupData(groupName);

    if (result != null && result.size() > 0)
    {
        // Iterate over the result
        for (java.util.Map.Entry<String, Customer> item : result.entrySet())
        {
            // Perform operations
        }
    }
    else
    {
        // No data against the group found
    }

    // Data with this group already exists in cache

    val groupName = "Important Customers"

    val result = cache.getSearchService.getGroupData(groupName)

    if (result != null && result.nonEmpty) {
      // Iterate over the result
      for (item <- result) {
        // Perform operations
      }
    }
    else {
      // No data against the group found
    }

    // This is an async method

    // Data with this group already exists in cache
    var groupName = "Important Customers";

    var searchService = await this.cache.getSearchService();
    var result = await searchService.getGroupData(groupName);

    if (result != null && result.size() > 0)
    {
        // Iterate over the result
        result.forEach(item => {

            // Perform Operations
        });               
    }
    else
    {
       // No data against the group found
    }

    # Data with this group already exists in cache
    group_name = "Important Customers"

    search_service = cache.get_search_service()
    result = search_service.get_group_data(group_name)

    if result is not None and len(result) > 0:
        # Iterate over the result
        for key in result:
            # Perform Operations
            print(result[key])

    else:
        # No data against the group found
        print("No data found")

Additional Resources

NCache provides a sample application for Groups on GitHub.

See Also

Add/Update Cache Data with Groups
Remove Cache Data with Group
Basic Operations for Caching Data
Tag Cache Data

Back to top Copyright © 2017 Alachisoft