Configure Searchable Attributes for Data Structures in Cache
A data structure can also be associated with the searchable attributes such as:
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.
Important
These attributes can not be modified once a data structure is created. However, the data structure itself can be modified.
Prerequisites
- To learn about the standard prerequisites required to work with all NCache client-side features, please refer to the given page on Client-Side API Prerequisites.
- For API details, refer to: ICache, DataTypeAttributes, NamedTagsDictionary, Tag, Group, NamedTags, IDistributedHashSet, DataTypeManager, CreateHashSet, Contains, Count.
The following code sample creates a HashSet ProductIDSet of int type and specifies Group Electronics, Tags and a Named Tag to it using DataTypeAttributes
. The ProductIDs are then fetched and added to this HashSet. You can also query on these set values.
Tip
One quick way to verify whether a data structure 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 structure exists in the cache.
// Precondition: 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 HashSet
int[] productIDs = FetchProductIDs();
foreach(var productID in productIDs)
{
// Add product IDs to HashSet
hashSet.Add(productID);
}
// You can also query on this HashSet using these attributes
Note
To ensure that 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 a sample application for configuring searchable attributes with data structures on GitHub.
See Also
.NET: Alachisoft.NCache.Client.DataTypes namespace.
Java: com.alachisoft.ncache.client.datastructures namespace.
Python: ncache.client.datastructures class.
Node.js: DataStructureManager class.