Try Playground
Show / Hide Table of Contents

Aggregate Dependency [Deprecated]

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

  • .NET
  • Java
  • Legacy API
  • 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.
  • 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.

Add Data to Cache with Aggregate Dependency

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
// 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
// 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
Product product1 = new Product();
product1.ProductID = 1001;
product1.ProductName = "Chai";
string key1 = "Product:" + product.ProductID;

// Add item
cache.Add(key1, product1);

// Create dependent object
Product product2 = new Product();
product2.ProductID = 1002;
product2.ProductName = "Coffee";
string key2 = "Product:" + product.ProductID;

// file dependency
string filepath = "D:\\tempProductList.txt";

// Initializing Aggregate Dependency; item dependent on key of product1
AggregateCacheDependency aggregateDependency = new AggregateCacheDependency();
aggregateDependency.Dependencies.Add(new FileDependency(filepath));
aggregateDependency.Dependencies.Add(new KeyDependency(key));

// Adding new item with AggregateCacheDependency
cache.Add(key2, product2, aggregateDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal);
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.

In This Article
  • Prerequisites
  • Add Data to Cache with Aggregate Dependency
  • Additional Resources
  • See Also

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Professional
  • 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 - 2025. All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top