Configure Searchable Attributes for Data Structures in Cache
Note
This feature is only available in NCache Enterprise Edition.
A Data type can also be associated with searchable attributes such as:
- Groups
- Tags
- Named Tags
These searchable attributes can be used to query all associated items, as explained in Querying Data Structures. For example, a CacheItem and HashSet can belong to the same group. These attributes are specified using the DataTypeAttributes
class.
Warning
These attributes can not be modified once a data type is created, however the data type itself can be modified.
Pre-requisites
- Install the following NuGet packages:
- Include the following namespaces in your application:
Alachisoft.NCache.Client.DataTypes
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Exceptions
- The application must be connected to cache before performing the operation.
- Cache must be running.
- For API details, refer to: ICache, Contains, Count, DataTypeAttributes.
- 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.
- To handle any unseen exceptions, refer to the Troubleshooting section.
The following code sample creates a HashSet ProductIDSet of int type and specifies group Electronics, tags and a Named Tag to it using DataTypeAttributes
. ProductIDs are then fetched and added to this HashSet. You can query on these set values.
Tip
One quick way to verify whether data type has been created is to use either properties of the Cache class:
Count
returns the number of items present in the cache.Contains
verifies if the specified key of the data type exists in the cache.
try
{
// Pre-condition: Cache must be connected
// Specify unique cache key for set
string key = "ProductIDSet";
// Specify attributes for hashset
var attributes = new DataTypeAttributes();
// Specify group "Electronics"
attributes.Group = "Electronics";
// Specify tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("2 Year Warranty");
tags[1] = new Tag("Stainless Steel");
attributes.Tags = tags;
// Specify Named Tag
var namedTag = new NamedTagsDictionary();
namedTag.Add("Discount", 0.4);
attributes.NamedTags = namedTag;
// Create hashset with attributes
IDistributedHashSet<int> hashSet = cache.DataTypeManager.CreateHashSet<int>(key, attributes);
// Adding Product IDs to Set
int[] productIDs = FetchProductIDs();
foreach(var productID in productIDs)
{
// Add product IDs to set
hashSet.Add(productID);
}
// You can query on this hashset using these attributes
}
catch (OperationFailedException ex)
{
// NCache specific exception
if(ex.ErrorCode == NCacheErrorCodes.KEY_ALREADY_EXISTS)
{
// An item with the same key already exists
}
else if (ex.ErrorCode == NCacheErrorCodes.CACHEITEM_IN_DATA_STRUCTURES)
{
// Data structures cannot be of CacheItem type
// CacheItems cannot be added in data structures
}
else
{
// NCache specific exception
// 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.
See Also
Using Counter in Cache
Configure Invalidation Attributes
Query on Data Structures
Remove Data Structure from Cache