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

Bulk Extensible Dependency Usage and Implementation

Note

This feature is only available in NCache Enterprise Edition for .NET Servers.

In extensible dependency, each dependency takes time to determine change which might cause expiration lag. With increase in the number of custom dependencies, expiration on each item takes time because of which some items remain in the cache way past their expiry.

To counter this issue, NCache provides another class called BulkExtensibleDependency where bulk of custom dependencies are evaluated at the same time. This helps boost the application's performance as the items that have expired are removed from the cache in time. BulkExtensibleDependency is provided at runtime through which user can evaluate data dependencies in bulk and call .Expire() on the dependencies that need to be removed from the cache.

Important

In case your business requirement is a method where an item needs to be individually evaluated instead of in a bulk, then use ExtensibleDependency. Otherwise, you will face performance issues.

Tip

Refer to Custom Cache Dependencies to get acquainted with all cache dependency custom methods provided by NCache.

Step 1: Implement BulkExtensibleDependency Class

The first step in introducing your own logic for Bulk Extensible Dependency is to implement the BulkExtensibleDependency class.

Prerequisites

  • Install either of the following NuGet packages in your application based on your NCahce edition:
    • Enterprise: Alachisoft.NCache.SDK
    • Professional: Alachisoft.NCache.Professional.SDK
  • To override the BulkExtensibleDependency class, include the Alachisoft.NCache.Runtime.Dependencies namespace in your application.
  • The class implementing BulkExtensibleDependency must be marked as Serializable along with any other parameters the process code might take in.
  • The project must be implemented as a Class Library (.dll) in Visual Studio. This will be deployed on the NCache cluster.

The following class implements your custom logic to remove multiple items at a time using bulk extensible dependency.

[Serializable]
public class BulkDependency : BulkExtensibleDependency
{ 
    // Class parameters 

    public BulkDependency(string connString, int ProductId)
    {
        connection = DriverManager.GetConnection(connString);
        connection.open();
        productID = ProductId;
    }

    private Dictionary<int,int> UnitsInStockStatus(List<int> productIds)
        {
            String queryString = "your_query_here";
            Dictionary<int, int> productsInfo = new Dictionary<int, int>();

            using (var cmd = new SqlCommand(queryString, connection))
            {
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        int units = 0;
                        Int32.TryParse(reader["UnitsInStock"].ToString(), out units);
                        productsInfo.Add((int)reader.GetValue(0), units);
                    }
                }
            }
            return productsInfo;
        }
    }

    public override void EvaluateBulk(IEnumerable<BulkExtensibleDependency> dependencies)
    {
        List<BulkDependency> dependencyList = new List<BulkDependency>();
        foreach (BulkDependency bulkDependency in dependencies)
        {
            dependencyList.Add(bulkDependency);
        }

        List<int> productIds = GetOutofStockProducts(dependencyList);
        var productsInfo = UnitsInStockStatus(productIds);

        foreach (BulkDependency bulkDependency in dependencyList)
        {
            if (productsInfo.ContainsKey(bulkDependency.productId))
            {
                if (productsInfo[bulkDependency.productId] != bulkDependency.unitsInStock)
                {
                    bulkDependency.Expire();
                }
            }
        }
    }

    private List<int> GetOutofStockProducts()
    {
        List<int> productIds = new List<int>();

        foreach(BulkDependency bulkDependency in dependencies)
            {
                productIds.Add(bulkDependency.productId);
            }
        }
        return productIds;
    }

    private Dispose()
    {
        // Dispose off all resources
    }
    // This class is to be deployed on NCache 
}
Important

Do not use this.Expire() to expire an object that isn't valid anymore. Instead, call the .Expire() method on the instance of dependency that needs to be expired. e.g. myDep.Expire()

Step 2: Implement BulkCustomDependencyProvider

To implement Bulk Extensible Dependency provider in your application, use the following code snippet.

Prerequisites

  • Install either of the following NuGet packages in your application based on your NCahce edition:
    • Enterprise: Alachisoft.NCache.SDK
    • Professional: Alachisoft.NCache.Professional.SDK
  • To utilize NCache APIs, include the following namespaces in your application:
    • Alachisoft.NCache.Runtime.Dependencies
    • Alachisoft.NCache.Runtime.CustomDependencyProviders
    • Alachisoft.NCache.Runtime
    • Alachisoft.NCache.Runtime.Exceptions
  • This should be a class library project using Microsoft Visual Studio.

A custom dependency provider for bulk extensible dependency implements your logic that you created in step 1 on server side. Here's how you can implement your own provider:

public class BulkCustomDependencyProvider : ICustomDependencyProvider
{     
    public void Init(IDictionary<string, string> parameters, string cacheName)
    {
        // Initialize cache and class parameters
    }

    public BulkDependency CreateDependency(string key, IDictionary<string, string> dependencyParameters)
    {
        int productId = 0;
        int units = 0;
        string connectionString = "";

        if (dependencyParameters != null)
        {
            if (dependencyParameters.ContainsKey("ProductID"))
                productId = Int32.Parse(dependencyParameters["ProductID"]);

            if (dependencyParameters.ContainsKey("UnitsAvailable"))
                units = Int32.Parse(dependencyParameters["UnitsAvailable"]);

            if (dependencyParameters.ContainsKey("ConnectionString"))
                connectionString = dependencyParameters["ConnectionString"];

            // Create bulk extensible dependency
            BulkDependency dependency = new BulkDependency(connectionString, productId, units);
            return dependency;
        }
    }
    public void Dispose ()
    {
        // Dispose off all resources 
    }
}

Step 3: Deploy Implementation on Cache

Deploy this class and all other dependent assemblies on NCache by referring to Deploy Providers in Administrator's Guide for help.

Step 4: Use Bulk Extensible Dependency

Once bulk extensible dependency class has been implemented and deployed, it is ready to be used in your application.

Prerequisites

  • Install either of the following NuGet packages in your application based on your NCahce edition:
    • Enterprise: Alachisoft.NCache.SDK
    • Professional: Alachisoft.NCache.Professional.SDK
  • To utilize NCache API, include the following namespaces in your application:
    • Alachisoft.NCache.Client
    • Alachisoft.NCache.Runtime.Dependencies
    • Alachisoft.NCache.Runtime.Exceptions
  • The BulkExtensibleDependency class must be deployed on cache.
  • The application must be connected to cache before performing the operation.
  • The cache must be running.
  • To ensure the operation is fail-safe, it is recommended to handle any exceptions within your application, as explained in Handling Exceptions

The following code shows how to add data into the cache using the Insert() with bulk dependency.

try
{   
    // Specify the connection string
    string connectionString = ConfigurationManager.AppSettings["connectionstring"];

    // Fetch the product to be added to the cache
    Product product = FetchProductFromDB(productId);

    // Specify the unique key of the item
    string key = $"Product:{product.ProductID}";

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

    // Create dictionary for dependency parameters
    IDictionary<string, string> param = new Dictionary<string, string>();
    param.Add("ProductID", products.Id.ToString());
    param.Add("ConnectionString", _connectionString);

    // Create Bulk Extensible Dependency using Provider and Add it to Cache Item 
    CustomDependency customDependency = new CustomDependency(ConfigurationManager.AppSettings["ProviderName"], param);
    cacheItem.Dependency = customDependency;

    // Add cacheItem to the cache with bulk dependency
    cache.Insert(key, cacheItem);
}
catch (OperationFailedException ex)
{
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
}
catch (Exception ex)
{
    // Any generic exception like ArgumentNullException or ArgumentException
}

Additional Resources

NCache provides sample application for bulk extensible dependency on GitHub.

See Also

Custom Dependencies
Sync Cache using Extensible Dependency
Sync Cache using Notify Extensible Dependency
Configure Custom Dependencies

Back to top Copyright © 2017 Alachisoft