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.
- 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: Cache, DataStructureAttributes, setGroup, Tag, setTags, DistributedHashSet, NamedTagsDictionary, setNamedTags, getDataStructuresManager, createHashSet, contains, getCount.
- 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: Cache, set_group, set_tags, NamedTagsDictionary, set_named_tags, create_list,
contains, get_count, DataStructureManager, get_data_structures_manager.
- 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: Cache, DataStructureAttributes, setGroup, NamedTagsDictionary, Tag, setNamedTags, getDataStructuresManager, createList, contains, getCount.
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
// Precondition: Cache must be connected
// Specify unique cache key for set
String key = "ProductIDSet";
// Specify attributes for HashSet
var attributes = new DataStructureAttributes();
// Specify group "Electronics"
attributes.setGroup("Electronics");
// Specify tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("2 Year Warranty");
tags[1] = new Tag("Stainless Steel");
attributes.setTags(Arrays.asList(tags));
// Specify Named Tag
var namedTag = new NamedTagsDictionary();
namedTag.add("Discount", 0.4);
attributes.setNamedTags(namedTag);
var writeThruOptions = null;
// Create HashSet with attributes
DistributedHashSet hashSet = cache.getDataStructuresManager().createHashSet(key, attributes, writeThruOptions, Integer.class);
// Adding Product IDs to HashSet
int[] productIDs = fetchProductIDs();
for (var productID : productIDs) {
// Add product IDs to HashSet
hashSet.add(productID);
}
// You can query on this HashSet using these attributes
# Precondition: Cache must be connected
# Specify unique cache key for set
key = "ProductIDSet"
# Specify attributes for hashset
attributes = DataStructureAttributes()
# Specify group "Electronics"
attributes.set_group("Electronics")
# Specify tags
tags = [ncache.Tag("2 Year Warranty"), ncache.Tag("Stainless Steel")]
attributes.set_tags(tags)
# Specify Named Tag
named_tag = ncache.NamedTagsDictionary()
named_tag.add("Discount", 0.4)
attributes.set_named_tags(named_tag)
# Create list with attributes
attr_list = cache.get_data_structures_manager().create_list(key, int, attributes)
# Adding Product IDs to Set
product_ids = fetch_product_ids()
for product_id in product_ids:
# Add product IDs to set
attr_list.add(product_id)
# You can query on this list using these attributes
// This is an async method
// Pre-condition: Cache must be connected
// Specify unique cache key for HashSet
var key = "ProductIDSet";
// Specify attributes for HashSet
var attributes = new ncache.DataStructureAttributes();
// Specify group "Electronics"
attributes.setGroup("Electronics");
// Specify tags
var tags = [
new ncache.Tag("2 Year Warranty"),
new ncache.Tag("Stainless Steel"),
];
attributes.setTags(tag);
// Specify Named Tag
var namedTag = new ncache.NamedTagsDictionary();
namedTag.add("Discount", 0.4);
attributes.setNamedTags(namedTag);
// Create list with attributes
var list = await (
await this.cache.getDataStructuresManager()
).createList(key, Number(), attributes, null);
// Adding Product IDs to HashSet
var productIDs = this.fetchProductIDs();
for (var productID in productIDs) {
// Add product IDs to HashSet
list.add(productID);
}
// You can query on this list 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.