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

Aggregate Cache Dependency

Note

This feature is only available in NCache Enterprise Edition.

NCache also allows you to use different strategies in combination with the same cache data in the form of Aggregate Cache Dependency. 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.

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 above dependencies together using aggregate dependency. All of these dependencies are explained in the successive chapters.

For example, there is a case when an item is dependent on another item in the cache as well as 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, aggregate dependency can be used here. It lets you add both key and file dependency to the item.

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. 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

  • .NET/.NET Core
  • Java
  • 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, CacheItem, Dependency, AggregateCacheDependency, FileDependency, KeyDependency.
  • 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, CacheItem, setDependency, AggregateCacheDependency, FileDependency, KeyDependency.

Add Data to Cache with Aggregate Dependency

You can use Add method to add aggregate dependency to an item in the cache.

Important

Note that this API also specifies cache item priority for eviction as well as expiration, so the value for that parameter has been passed as Default, as it is not discussed here.

The following example adds a CacheItem to the cache against a key that is dependent on another item in the cache as well as a file named productList.csv placed at the specified path using aggregate dependency.

  • .NET/.NET Core
  • Java
try
{
      // Pre-condition: 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 key1 = "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(key1, 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 = "Product:" + product.productID;

      // Create a new cache item of this product
      CacheItem cacheItem = new CacheItem(product);

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

      // Initializing Aggregate dependency
      AggregateCacheDependency aggregateCacheDependency = new AggregateCacheDependency();

      // Adding file and key dependency in aggregate dependency
      aggregateCacheDependency.add(new FileDependency(filePath));
      aggregateCacheDependency.add(new KeyDependency(key));

      // Adding cache item in the cache with aggregate dependency
      cacheItem.setDependency(aggregateCacheDependency);
      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
}
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 sample application for File Dependency on GitHub.

See Also

Key Dependency Types and Usage
Multi Cache Key Dependency
Data Expiration
Cache Data Dependency on External Source

Back to top Copyright © 2017 Alachisoft