Retrieve Cache Data with Groups
Note
This feature is only available in NCache Enterprise Edition.
Once the items are added to the specified groups, the user can also retrieve certain cache items or keys associated with that group.
Pre-Requisites
- Include the following namespace in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime
Alachisoft.NCache.Runtime.Exceptions
- The application must be connected to cache before performing the operation.
- Cache must be running.
- Make sure that the data being added is serializable.
- 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 of a Particular Group
To return list of keys that belongs to a specified group, you can use the GetGroupKeys method. This method returns a list of keys mapped under the group passed to this API. For Java, use the getGroupKeys method to retrieve the keys mapped under the specific group.
Important
- Passing empty strings for group will return empty result set.
- Passing only the value of group will return all keys mapped under the group.
- Passing null to this API for group will throw ArgumentNullException.
try
{
// Data with this group already exists in cache
string groupName = "Important Customers";
ICollection<string> retrievedKeys = cache.SearchService.GetGroupKeys(groupName);
if (retrievedKeys != null && retrievedKeys.Count > 0)
{
// Iterate over key result
foreach (var key in 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 ArgumentNullException or ArgumentException
}
Recommendation: 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 keys and 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. For Java, use the getGroupData to retrieve the data(keys and values) of the group.
Important
- Passing empty strings for group will return empty result set.
- Passing null for group will throw ArgumentNullException.
- Passing only the value of group will return all keys and values mapped under the group.
The following example retrieves the keys and the values that belong to the group Important Customers.
try
{
// Data with this group already exists in cache
string groupName = "Important Customers";
IDictionary<string, Customer> result =
cache.SearchService.GetGroupData<Customer>(groupName);
if (result != null && result.Count > 0)
{
// Iterate over hashset
foreach (KeyValuePair<string, Customer> item in result)
{
// 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 ArgumentNullException or ArgumentException
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Additional Resources
NCache provides sample application for Groups at:
- GitHub
- Shipped with NCache: %NCHOME%\samples\dotnet\GroupsAndTags
See Also
Add/Update Cache Data with Groups
Remove Cache Data with Group
Basic Operations for Caching Data
Tag Cache Data