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
- 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
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 = null;
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:
# Perform Operations
print(key)
else:
# No data against the group found
print("No keys found")
// Precondition: Cache is connected
// This is an async method
// Data with this group already exists in cache
var groupName = "West Coast 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
}
// 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
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
String groupName = "West Coast Customers";
Map<String, Customer> retrievedDictionary = null;
retrievedDictionary = cache.getSearchService().<Customer>getGroupData(groupName);
if (retrievedDictionary != null && !retrievedDictionary.isEmpty()) {
for (java.util.Map.Entry<String, Customer> retrievedItem : retrievedDictionary.entrySet()) {
System.out.println("Customer '" + retrievedItem.getValue().getCustomerID() + "' having Key '" + retrievedItem.getKey() + "' belongs to West Coast");
}
}
# Precondition: Cache is connected
# 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")
// This is an async method
// Precondition: Cache is connected
// 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
}
// 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.