• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Try Free
Show / Hide Table of Contents

Configure Searchable Attributes for Data Structures in Cache

Note

This feature is only available in the NCache Enterprise Edition.

A data structure can also be associated with the 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.

Important

These attributes can not be modified once a data structure is created. However, the data structure itself can be modified.

Prerequisites

  • .NET/.NET Core
  • Java
  • Scala
  • Node.js
  • Python
  • 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, DataStructureAttributes, Tag, getDataStructuresManager, count, contains.
  • 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.
  • 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 detais refer to: Cache, set_group, set_tags, NamedTagsDictionary, set_named_tags, create_list, contains, get_count, DataStructureManager, get_data_structures_manager.

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 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.
  • .NET/.NET Core
  • Java
  • Scala
  • Node.js
  • Python
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 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
}
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
}
try {
    // Pre-condition: 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
} catch (OperationFailedException ex) {
    // NCache specific exception
    if (ex.getErrorCode() == NCacheErrorCodes.KEY_ALREADY_EXISTS) {
        // An item with the same key already exists
    } else if (ex.getErrorCode() == 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 IllegalArgumentException or NullPointerException
}
try {
    import java.util
    // Pre-condition: Cache must be connected// Pre-condition: Cache must be connected

    // Specify unique cache key for set
    val key = "ProductIDSet"

    // Specify attributes for hashset
    val attributes = new DataStructureAttributes

    // Specify group "Electronics"
    attributes.setGroup("Electronics")

    // Specify tags
    val tags = List(Tag("2 Year Warranty"), Tag("Stainless Steel"))
    attributes.setTags(tags)

    // Specify Named Tag
    val namedTag = new NamedTagsDictionary
    namedTag.add("Discount", 0.4)
    attributes.setNamedTags(namedTag)

    val writeThruOptions = null

    // Create hashset with attributes
    val hashSet = cache.getDataStructuresManager.createHashSet(key, attributes, writeThruOptions, classOf[String])

    // Adding Product IDs to Set
    val productIDs = fetchProductIDs

    for (productID <- productIDs) { // Add product IDs to set
      hashSet.add(productID)
    }

    // You can query on this hashset using these attributes
}
catch {
    case exception: Exception => {
      // Handle any errors
    }
}
// This is an async method
try {
  // 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
} catch (error) {
  // Handle errors
}
try:
    # Pre-condition: 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
except Exception as exp:
    # Handle errors
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

Using Counter in Cache
Configure Invalidation Attributes
Query on Data Structures
Remove Data Structure from Cache

Back to top Copyright © 2017 Alachisoft