Retrieve Cache Data with Groups [Deprecated]
Once the items are added to the specific groups in cache, the user can also retrieve certain cache items or keys associated with that group.
Prerequisites
- Create a new Console Application.
- Install either of the following NuGet packages in your .NET client application:
- Enterprise:
Install-Package Alachisoft.NCache.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 namespace 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.
Retrieve Keys of a Particular Group
To return a list of keys that belong to a specified group in cache, you can use 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.
// Precondition: Cache is connected
// Data with this group already exists in cache
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.");
}
}
// Precondition: Cache is connected
// Data with this group already exists in cache
String groupName = "West Coast Customers";
Collection<String> keys = cache.getSearchService().getGroupKeys(groupName);
if (keys != null && !keys.isEmpty())
{
// Iterate over the result
for (String key : keys)
{
System.out.println("Key '" + key + "' belongs to '" + groupName + "' group.");
}
}
# Precondition: Cache is connected
# Data with this group already exists in cache
group_name = "West Coast 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:
print(f"Key '{key}' belongs to '{group_name}' group.")
// Precondition: Cache is connected
// This is an async method
// Data with this group already exists in cache
const groupName = "West Coast Customers";
const searchService = await cache.getSearchService();
const retrievedKeys = await searchService.getGroupKeys(groupName);
if (retrievedKeys != null && retrievedKeys.length > 0)
{
// Iterate over the result
for (const key of retrievedKeys)
{
console.log(`Key '${key}' belongs to '${groupName}' group.`);
}
}
// Using NCache Enterprise 4.9.1
// Precondition: Cache is connected
// Data with this group already exists in cache
string groupName = "West Coast Customers";
string subGroupName = "Retail"; // Example subgroup
// Fetch keys from cache using legacy API for both group and subgroup
ArrayList keys = cache.GetGroupKeys(groupName, subGroupName);
if (keys != null && keys.Count > 0)
{
foreach (object key in keys)
{
Console.WriteLine("Key '" + key + "' belongs to group '" + groupName + "' and subgroup '" + subGroupName + "'.");
}
}
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 in cache, 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.
// Precondition: Cache is connected
// Data with this group already exists in cache
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");
}
}
// Precondition: Cache is connected
// Data with this group already exists in cache
String groupName = "West Coast Customers";
Map<String, Object> retrievedDictionary = cache.getSearchService().getGroupData(groupName);
if (retrievedDictionary != null && !retrievedDictionary.isEmpty())
{
for (Map.Entry<String, Object> retrievedItem : retrievedDictionary.entrySet())
{
Object value = retrievedItem.getValue();
if (value instanceof Customer)
{
Customer customer = (Customer) value;
System.out.println("Customer '" + customer.getContactName() + "' having Key '" + retrievedItem.getKey() + "' belongs to West Coast");
}
}
}
# Precondition: Cache is connected
# Data with this group already exists in cache
group_name = "West Coast Customers"
search_service = cache.get_search_service()
retrieved_dict = search_service.get_group_data(group_name)
if retrieved_dict is not None and len(retrieved_dict) > 0:
for key, customer_map in retrieved_dict.items():
customer = Customer(
customer_id=customer_map.get("customer_id"),
contact_name=customer_map.get("name"),
region=customer_map.get("region")
)
print(f"Customer '{customer.contact_name}' having Key '{key}' belongs to West Coast")
// This is an async method
// Precondition: Cache is connected
// Data with this group already exists in cache
const groupName = "West Coast Customers";
const searchService = await cache.getSearchService();
const retrievedDictionary = await searchService.getGroupData(groupName);
if (retrievedDictionary != null)
{
retrievedDictionary.forEach((customer, key) =>
{
console.log(`Customer '${customer.ContactName}' having Key '${key}' belongs to ${groupName}`);
});
}
// Using NCache Enterprise 4.9.1
// Precondition: Cache is connected
string groupName = "West Coast Customers";
string subGroupName = "Retail";
ArrayList keys = cache.GetGroupKeys(groupName, subGroupName);
if (keys != null && keys.Count > 0)
{
foreach (string key in keys)
{
Customer customer = cache.Get(key) as Customer;
if (customer != null)
{
Console.WriteLine($"Customer '{customer.ContactName}' having Key '{key}' belongs to '{groupName}'");
}
}
}
Additional Resources
NCache provides a sample application for Groups on GitHub.
See Also
.NET: Alachisoft.NCache.Client namespace.
Java: com.alachisoft.ncache.client namespace.
Python: ncache.client class.
Node.js: Cache class.