Retrieve Cache Data with Groups [Deprecated]
Once the items are added to the specific groups in cache data, the user can also retrieve certain cache items or keys associated with that group.
Prerequisites to Retrieve Cache Data with Groups
- 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.
Retrieve Keys of a Particular Group
To return a list of keys that belong to a specified group in cache data, 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.
// Preconditions: 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.");
}
}
// Precondition: Cache is connected
// A user wants to get the cache keys of all the customers from the West Coast
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 = "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")
// Preconditions: Cache is connected
// This is an async method
// 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
}
string groupName = "Product";
string subGroupName = "Beverages";
ArrayList keys = cache.GetGroupKeys(groupName, subGroupName);
foreach (object obj in keys)
//do something
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 data, 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.
// 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");
}
}
// 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";
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");
}
}
# 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
// 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
}
string groupName = "Product";
string subGroupName = "Beverages";
ArrayList keys = cache.GetGroupKeys(groupName, subGroupName) as IDictionary;
if (data.Count > 0)
{
IDictionaryEnumerator result = data.GetEnumerator();
while (result.MoveNext())
{
string key = (string)result.Key;
Product val = (Product)result.Value;
//do something
}
}
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.