• Facebook
  • Twitter
  • Youtube
  • LinedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Download
  • Contact Us
Download
Show / Hide Table of Contents

Configure Searchable Attributes for Data Structures in Cache

NCache allows you to organize distributed data structures, such as HashSets, Lists, and Queues, by associating them with searchable metadata. A data structure can 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.

Important

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

Prerequisites

Before using the NCache client-side APIs, ensure that the following prerequisites are fulfilled:

  • .NET
  • Java
  • Node.js
  • Install the following NuGet packages in your .NET client application:
    • Enterprise: Alachisoft.NCache.SDK
    • OpenSource: Alachisoft.NCache.Opensource.SDK
  • Include the following namespaces in your application:
    • Alachisoft.NCache.Client
    • Alachisoft.NCache.Runtime.Exceptions
    • Alachisoft.NCache.Client.DataTypes
    • Alachisoft.NCache.Client.DataTypes.Collections
    • Alachisoft.NCache.Runtime.Caching
    • Alachisoft.NCache.Runtime.Events
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • For API details, refer to: ICache, DataTypeAttributes, NamedTagsDictionary, Tag, Group, NamedTags, IDistributedHashSet, DataTypeManager, CreateHashSet, Contains, Count.
  • Add the following Maven dependencies for your Java client application in pom.xml file:
<dependency>
    <groupId>com.alachisoft.ncache</groupId>
    <!--for NCache Enterprise-->
    <artifactId>ncache-client</artifactId>
    <version>x.x.x</version>
</dependency>
  • Import the following packages in your Java client application:
    • import com.alachisoft.ncache.client.*;
    • import com.alachisoft.ncache.runtime.exceptions.*;
    • import com.alachisoft.ncache.client.datastructures.*;
    • import com.alachisoft.ncache.events.*;
    • import com.alachisoft.ncache.runtime.dependencies.*;
    • import com.alachisoft.ncache.runtime.caching.*;
    • import com.alachisoft.ncache.runtime.caching.NamedTagsDictionary.*;
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • For API details, refer to: Cache, DataStructureAttributes, setGroup, Tag, setTags, DistributedHashSet, NamedTagsDictionary, setNamedTags, getDataStructuresManager, createHashSet, contains, getCount.
  • Install and include the following module in your Node.js client application:

    • Enterprise: ncache-client
  • Include the following class in your application:

    • Cache
    • DataStructureManager
    • Tag
    • NamedTagsDictionary
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • 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.
  • .NET
  • Java
  • Node.js
try
{
  // Precondition: Cache is already 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
{
  // Precondition: Cache is already 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);
  WriteThruOptions 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
{
  // Precondition: Cache is already connected
  // This is an async method

  // Specify unique cache key for list
  var key = "ProductIDSet";

  // Specify attributes for list
  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(tags);

  // 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 list
  var productIDs = this.fetchProductIDs();
  for (var productID in productIDs) {
  // Add product IDs to list
  list.add(productID);
  }
  // You can query on this list using these attributes
}
catch (error) {
  // 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

.NET: Alachisoft.NCache.Client.DataTypes namespace
Java: com.alachisoft.ncache.client.datastructures namespace
Node.js: DataStructureManager class

Contact Us

PHONE

+1 214-619-2601   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top