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

Configure Invalidation Attributes for Data Structures in Cache

NCache provides invalidation mechanisms for distributed data structures that control their lifetime and removal behavior in the cache. These mechanisms are configured through the DataTypeAttributes class at the time of data structure creation and apply to all supported distributed data structures. Since these settings define the core behavior of the data structure, they must be specified during initialization and cannot be changed afterward.

The data structures can be invalidated from the cache using:

  • Expiration
  • Eviction
  • Dependencies

These attributes are specified during the data structure creation using the DataTypeAttributes class and cannot be modified once a data structure is created in the cache. Moreover, these invalidation attributes can be specified against all data structures.

Note

Key dependencies can also be configured between the CacheItem and data structures.

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
    • Alachisoft.NCache.Runtime.Dependencies
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • For API details, refer to: ICache, DataTypeAttributes, CacheItemPriority, ExpirationType, KeyDependency, IDistributedHashSet, IDataTypeManager, CreateHashSet.
  • 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.*;
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • For API details, refer to: Cache, DataStructureAttributes, CacheItemPriority, setPriority, ExpirationType, setExpiration, setDependency, KeyDependency, getDataStructuresManager, createHashSet.
  • Install and include the following module in your Node.js client application:

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

    • Cache
    • DataStructureManager
    • KeyDependency
    • Expiration
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • For API details, refer to: Cache, DataStructureAttributes, CacheItemPriority, setPriority, Expiration, ExpirationType, setExpiration, KeyDependency, setDependency, getDataStructuresManager, createList.

Use Invalidation Attributes with Data Structures

The following code sample creates a set OrderIDSet that is dependent on a Product object in the cache. The OrderIDSet is assigned an expiration value of 15 minutes and its priority is set to high. If the Product is modified in the cache, OrderIDSet is removed from the cache.

  • .NET
  • Java
  • Node.js
try
{
  // Precondition: Cache is already connected

  // Product exists in cache with key "Product:1001"
  string productKey = "Product:1001";

  // Specify attributes for HashSet
  var attributes = new DataTypeAttributes();

  // Specify priority for eviction
  attributes.Priority = CacheItemPriority.High;

  // Specify Absolute expiration of 15 minutes
  var expiration = new Expiration(ExpirationType.Absolute, TimeSpan.FromMinutes(15));
  attributes.Expiration = expiration;

  // Specify key dependency of orderKey on customerKey
  var keyDependency = new KeyDependency(productKey);
  attributes.Dependency = keyDependency;

  // Create HashSet which is dependent on product:1001
  // Specify unique cache key for set
  string orderKey = "OrderIDSet";
  IDistributedHashSet<int> orderIDSet = cache.DataTypeManager.CreateHashSet<int>(orderKey, attributes);

  // Check if the orderIDSet exists in cache:
  // After 15 minutes or
  // If product is modified in cache
}
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

  // Product exists in cache with key "Product:1001"
  String productKey = "Product:1001";

  // Specify attributes for HashSet
  var attributes = new DataStructureAttributes();

  // Specify priority for eviction
  attributes.setPriority(CacheItemPriority.High);

  // Specify Absolute expiration of 15 minutes
  var expiration = new Expiration(ExpirationType.Absolute, TimeSpan.FromMinutes(15));
  attributes.setExpiration(expiration);

  // Specify key dependency of orderKey on customerKey
  var keyDependency = new KeyDependency(productKey);
  attributes.setDependency(keyDependency);

  // Create HashSet which is dependent on product:1001
  // Specify unique cache key for set
  String orderKey = "OrderIDSet";
  WriteThruOptions writeThruOptions = null;

  // Create HashSet
  DistributedHashSet orderIDSet = InitializeCache.cache.getDataStructuresManager().createHashSet(orderKey, attributes, writeThruOptions, Integer.class);

  // Check if the orderIDSet exists in cache:
  // After 15 minutes or
  // If product is modified in cache
}
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

  // Product exists in cache with key "Product:1001"
  var productKey = "Product:1001";

  // Specify attributes for list
  var attributes = new ncache.DataStructureAttributes();

  // Specify priority for eviction
  attributes.setPriority(ncache.CacheItemPriority.High);

  // Specify Absolute expiration of 15 minutes
  var expiration = new ncache.Expiration(
  ncache.ExpirationType.Absolute,
  ncache.TimeSpan.fromMinutes(15)
  );
  attributes.setExpiration(expiration);

  // Specify key dependency of orderKey on customerKey
  var keyDependency = new ncache.KeyDependency(productKey);
  attributes.setDependency(keyDependency);

  // Create list which is dependent on product:1001
  // Specify unique cache key for list
  var orderKey = "OrderIDSet";
  var orderIDList = await (
  await this.cache.getDataStructuresManager()
  ).createList(orderKey, Number(), attributes, null);

  // Check if the orderIDSet exists in cache:
  // After 15 minutes or
  // If product is modified in cache
}
catch (error) {
  // Handle errors
}
Note

To ensure 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 invalidation 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