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

Aggregate Dependency [Deprecated]

The Aggregate Cache Dependency allows you to associate multiple dependencies of different types with a single cached item. For example, you can associate Key Dependency and File Dependency with an item using Aggregate Dependency and data will be invalidated based on whichever dependency triggers first.

The following are the dependencies that can be added using aggregate dependencies:

  • Key Dependency
  • File Dependency
  • Database Cache Dependency
    • Notification-based Dependency
    • Polling-based Dependency

You can add any of the above dependencies together using Aggregate Dependency. For example, there is a case when an item is dependent on another item in the cache as well as on a file placed at some path outside the cache. Since the item is dependent on both, the key of the other item and a file, the Aggregate Dependency can be used here. It lets you add both Key and File Dependency to the item.

The Aggregate Cache Dependency associates one cached item with a collection of dependent objects which can be any other dependency provided by NCache, e.g., KeyDependency, DBCacheDependency, or any combination of these. The AggregateCacheDependency monitors a collection of dependency objects so that when any of them changes, the cached object becomes obsolete and is removed from the cache. Combining different invalidation strategies provides you with more flexible ways to meet your application needs in different environments.

Prerequisites

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

  • .NET
  • Java
  • Legacy API
  • 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.Dependencies
    • Alachisoft.NCache.Runtime.Exceptions
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • For API details, refer to: ICache, CacheItem, Dependency, AggregateCacheDependency, FileDependency, KeyDependency.
  • 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.dependencies.*;
    • import com.alachisoft.ncache.runtime.exceptions.*;
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • For API details, refer to: Cache, CacheItem, setDependency, AggregateCacheDependency, FileDependency, KeyDependency.
  • Install either of the following NuGet packages in your .NET client application:
    • Enterprise: Install-Package Alachisoft.NCache.SDK -Version 4.9.1.0
  • Create a new Console Application.
  • Make sure that the data being added is serializable.
  • Add NCache References by locating %NCHOME%\NCache\bin\assembly\4.0 and adding Alachisoft.NCache.Web and Alachisoft.NCache.Runtime as appropriate.
  • Include the Alachisoft.NCache.Runtime.Dependencies namespace in your application.
  • To learn more about the NCache Legacy API, please download the NCache 4.9 documents available as a .zip file on the Alachisoft Website.

Add Data to Cache

You can add items to your cache using the Aggregate Dependency. The following example adds a CacheItem to the cache against a key that is dependent on another item in the cache as well as on a file named productList.csv placed at the specified path using Aggregate Dependency.

  • .NET
  • Java
  • Legacy API
try
{
  // Precondition: Cache is already connected

  // Item with the given key exists in the cache
  string key = "Product:1001";

  // Get product from database against given ProductID
  Product product = FetchProductFromDB(1001);

  // Generate a unique cache key for this product
  string dependentKey = $"Product:" + {product.ProductID};

  // Create a new CacheItem with product
  var cacheItem = new CacheItem(product);

  // Specify the path where the master file is placed
  string filepath = "D:\\ProductList.csv";

  // Initializing Aggregate Dependency
  var aggregateDependency = new AggregateCacheDependency();

  // Adding file and key dependency in Aggregate dependency
  aggregateDependency.Dependencies.Add(new FileDependency(filepath));
  aggregateDependency.Dependencies.Add(new KeyDependency(key));

  // Adding new item with AggregateCacheDependency
  cacheItem.Dependency = aggregateDependency;
  cache.Insert(dependentKey, cacheItem);

  // For successful addition of item with dependency
  // Update or remove the master key or file
  // Verify if the dependent key is present
}
catch (OperationFailedException ex)
{
      // NCache specific exception
      if (ex.ErrorCode == NCacheErrorCodes.DEPENDENCY_KEY_NOT_FOUND)
      {
            // The dependent item does not exist in the cache
      }
      else
      {
            // 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

  // Item with the given key exists in the cache
  String key = "Product:1001";

  // Get product from database against given productId
  Product product = fetchProductFromDb(productId);

  // Generate a unique cache key for this product
  String productKey = String key = "Product:" + product.ProductID;

  // Create a new cacheItem with product
  CacheItem cacheItem = new CacheItem(product);

  // Specify the path where the master file is placed
  String filePath = "ProductList.csv";

  // Initializing Aggregate Dependency
  AggregateCacheDependency aggregateDependency = new AggregateCacheDependency();

  // Adding file and key dependency in Aggregate Dependency
  aggregateDependency.Dependencies.Add(new FileDependency(filePath));
  aggregateDependency.Dependencies.Add(new KeyDependency(key));

  // Adding cache item in the cache with Aggregate dependency
  cacheItem.Dependency = aggregateDependency;
  cache.insert(productKey, cacheItem);

  // For successful addition of item with dependency
  // Update or remove the master key or file
  // Verify if the dependent key is present
}
catch (OperationFailedException exception) 
{
      // NCache specific exception
      if (exception.getErrorCode() == NCacheErrorCodes.DEPENDENCY_KEY_NOT_FOUND) 
      {
            // The dependent item does not exist in the cache
      } 
      else
      {
            // Exception can occur due to:
            // Connection Failures
            // Operation Timeout
            // Operation performed during state transfer
      }
}
catch (Exception exception) {
      // Any generic exception like IllegalArgumentException or NullPointerException
}
try
{
  // Using NCache Enterprise 4.9.1
  // Precondition: Cache is already connected

  // Item with the given key exists in the cache
  string key = "Product:1001";

  // Get product from database against given ProductID
  Product product = FetchProductFromDB(1001);

  // Generate a unique cache key for this product
  string dependentKey = $"Product:{product.ProductID}";

  // Create a new CacheItem with product
  var cacheItem = new CacheItem(product);

  // Specify the path where the master file is placed
  string filePath = "D:\\ProductList.csv";

  // Initializing Aggregate Dependency
  AggregateCacheDependency aggregateDependency = new AggregateCacheDependency();

  // Adding file and key dependency in Aggregate Dependency
  aggregateDependency.Dependencies.Add(new FileDependency(filePath));
  aggregateDependency.Dependencies.Add(new KeyDependency(key));

  // Adding new item with AggregateCacheDependency
  cacheItem.Dependency = aggregateDependency;
  cache.Insert(dependentKey, cacheItem);

  // For successful addition of item with dependency
  // Update or remove the master key or file
  // Verify if the dependent key is present
}
catch (OperationFailedException ex)
{
    // NCache specific exception
    if (ex.ErrorCode == NCacheErrorCodes.DEPENDENCY_KEY_NOT_FOUND)
    {
        // The dependent key does not exist in cache
    }
    else
    {
        // Possible causes:
        // - Connection failure
        // - Operation timeout
        // - State transfer in progress
    }
}
catch (Exception ex)
{
    // Generic fallback for any unexpected exception
}
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 File Dependency on GitHub.

See Also

.NET: Alachisoft.NCache.Runtime.Dependencies namespace.
Java: com.alachisoft.ncache.runtime.dependencies namespace.

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