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

Implement Cache Loader and Refresher

Note

This feature is only available in NCache Enterprise Edition.

To use Cache Startup Loader and Refresher, the ICacheLoader interface needs to be implemented first. Then the custom logic of loader and refresher can be configured using NCache Web Manager or NCache PowerShell cmdlet. NCache loads and refreshes data from the configured data source based on the custom logic. On cache startup, NCache calls the Init method of the Cache Startup Loader to initialize it and upon successful initialization of the cache startup loader, calls the implemented LoadDatasetOnStartup method to load data into the cache. It then uses the RefreshDataset method to refresh the data loaded in the cache on the determined refresh-interval.

In the following example, the ICacheLoader interface is implemented to configure a custom loader and refresher logic. Let's suppose you configured two datasets from NCache Web Manager: Products and Suppliers. The following implementation loads products and suppliers on cache startup, refreshes these datasets on their specified refresh time.

For more details on the components of Cache Loader, refer to the chapter Components of Cache Startup Loader and Refresher.

Note

Before implementing cache startup loader and refresher, make sure that:

  • Loader service is running.
  • Firewall is disabled.

Prerequisites

  • .NET/.NET Core
  • Java
  • Install the following NuGet package in your application:
    • Enterprise: Alachisoft.NCache.SDK
  • Include the following namespaces in your application:
    • Alachisoft.NCache.Client
    • Alachisoft.NCache.Runtime.CacheLoader
    • Alachisoft.NCache.Runtime.Caching
    • Alachisoft.NCache.Runtime.Dependencies
  • Data being added into cache must be marked as serializable.
  • For API details, refer to: ICache, CacheItem, ICacheLoader.
  • This should be a class library project.
  • Make sure to configure the Cache Loader using NCache Web Manager or PowerShell cmdlet on NCache cluster.
  • Add the following Maven dependencies in your pom.xml file:
<dependency>
    <groupId>com.alachisoft.ncache</groupId>
    <artifactId>ncache-client</artifactId>
    <version>x.x.x</version>
</dependency>
  • Import the following packages in your application:
    • import com.alachisoft.ncache.client.*;
    • import com.alachisoft.ncache.runtime.cacheloader.*;
    • import com.alachisoft.ncache.runtime.exceptions.*;
  • Data being added into cache must be marked as serializable.
  • For API details, refer to:Cache, CacheItem, CacheLoader.
  • This should be a class library project.
  • Make sure to configure the Cache Loader using NCache Web Manager or PowerShell cmdlet on NCache cluster.

Initialize Cache Startup Loader and Refresher

The following code shows how to implement the Init method of the ICacheLoader interface. This method takes parameters as input and assigns values against them.

  • .NET/.NET Core
  • Java
public void Init(IDictionary<string, string> parameters, string cacheName)
{
    cache = CacheManager.GetCache(cacheName);
    connectionString = parameters.ContainsKey("ConnectionString") ? parameters["ConnectionString"] : null;
    if (connectionString != null)
    {
        connection = new SqlConnection(connectionString);
    }
}
public void init(Map<String, String> parameters, String cacheName) {
    cache = CacheManager.getCache(cacheName);
    connectionString = parameters.containsKey("ConnectionString") ? parameters.get("ConnectionString") : null;
    if (connectionString != null) {
        connection = DriverManager.getConnection(connectionString);
    }
}

Load Data on Cache Startup

The following implementation of LoadDatasetOnStartup method fetches a product from data source and adds it to the cache. The LoadDatasetOnStartup returns a user context that holds the information about the data loaded in the cache. This method is called when the cache starts to preload data in cache.

  • .NET/.NET Core
  • Java
public object LoadDatasetOnStartup(string dataset)
{
    // Create a list of datasets to load at cache startup
    IList<object> datasetToLoad;

    switch (dataset.ToLower())
    {
        // If dataset is products, fetch products from data source to load in cache
        case "products":
            datasetToLoad = FetchProductsFromDataSource();

            // Insert fetched products in the cache
            foreach (var product in datasetToLoad)
            {
                string key = $"ProductID:{product.Id}";
                cache.Insert(key, product);
            }
            break;

        // If dataset is suppliers, fetch suppliers from data source to load in cache    
        case "suppliers":
            datasetToLoad = FetchSuppliersFromDataSource();

            // Insert fetched suppliers in the cache
            foreach (var supplier in datasetToLoad)
            {
                string key = $"SupplierID:{supplier.Id}";
                cache.Insert(key, supplier);
            }
            break;

        default:
            // Invalid dataset
    }
    // User context is the time at which datasets were loaded in the cache
    object userContext = DateTime.Now; 
    return userContext;
}
public Object loadDatasetsOnStartup(String dataset) {
    // Create a list of datasets to load at cache startup
    List<Object> datasetToLoad;

    switch (dataset.toLowerCase()) {
        // If dataset is products, fetch products from data source and add in cache
        case "products":
            datasetToLoad = fetchProductsFromDataSource();

            // Insert fetched products in the cache
            for (var product : datasetToLoad) {
                string key = "ProductID:" + product.productID;
                cache.insert(key, product);
            }
            break;

        // If dataset is suppliers, fetch suppliers from data source and add in cache
        case "suppliers":
            datasetToLoad = fetchSuppliersFromDataSource();

            // Insert fetched suppliers in the cache
            for (var supplier : datasetToLoad) {
                string key = "SupplierID:" + supplier.supplierID;
                cache.insert(key, supplier);
            }
            break;

        default:
            // Invalid dataset
    }
    // User context is the time at which datasets were loaded in the cache
    Object userContext = LocalDateTime.now();
    return userContext;
}

Refresh Dataset

The following code implements how to refresh the data that has been loaded in the cache on startup whenever Cache Refresher is invoked. The RefreshDataset method uses the user context returned by the LoadDatasetOnStartup method to verify which data to refresh.

  • .NET/.NET Core
  • Java
public object RefreshDataset(string dataset, object userContext)
{
    DateTime? lastRefreshTime;
    switch (dataset.ToLower())
    {
        // If dataset is products, fetch updated products from data source
        case "products":
            lastRefreshTime = userContext as DateTime?;
            IList<Product> productsToRefresh = FetchUpdatedProducts(lastRefreshTime) as IList<Product>;

            // Insert updated products in the cache
            foreach (var product in productsToRefresh)
            {
                string key = $"ProductID:{product.Id}";
                CacheItem cacheItem = new CacheItem(product);
                _cache.Insert(key, cacheItem);
            }
            break;

        // If dataset is supplier, fetch updated suppliers from data source
        case "suppliers":
            lastRefreshTime = userContext as DateTime?;
            IList<Supplier> suppliersToRefresh = FetchUpdatedSuppliers(lastRefreshTime) as IList<Supplier>;

            // Insert updated suppliers in the cache
            foreach (var supplier in suppliersToRefresh)
            {
                string key = $"SupplierID:{supplier.Id}";
                CacheItem cacheItem = new CacheItem(supplier);
                _cache.Insert(key, cacheItem);
            }
            break;

        default:
            // Invalid dataset
    }
    // User context is the time at which datasets were refreshed
    userContext = DateTime.Now;
    return userContext;
}
public Object refreshDataset(String dataset, Object userContext) {
    @Nullable LocalDateTime lastRefreshTime;

    switch (dataset.toLowerCase()) {
        // If dataset is products, fetch updated products from the data source
        case "products":
            lastRefreshTime = (LocalDateTime) userContext;
            List<Product> productsToRefresh = (List<Product>) fetchUpdatedProducts(lastRefreshTime);

            // Insert updated products in the cache
            for (var product : productsToRefresh) {
                String key = "ProductID:" + product.productID;
                CacheItem cacheItem = new CacheItem(product);
                cache.insert(key, cacheItem);
            }
            break;

        // If dataset is suppliers, fetch updated suppliers from data source
        case "suppliers":
            lastRefreshTime = (LocalDateTime) userContext;
            List<Supplier> suppliersToRefresh = (List<Supplier>) fetchUpdatedSuppliers(lastRefreshTime);

            // Insert updated suppliers in the cache
            for (var supplier : suppliersToRefresh) {
                String key = "SupplierID:" + supplier.supplierID;
                CacheItem cacheItem = new CacheItem(supplier);
                cache.insert(key, cacheItem);
            }
            break;

        default:
            // Invalid dataset
    }
    // User context is the time at which the datasets are refreshed
    userContext = LocalDateTime.now();
    return userContext;
}

Get Datasets To Refresh

The GetDatasetsToRefresh method implements the custom logic to refresh a pre-configured dataset at runtime based on RefreshPreference . This method takes user context and assigns RefreshPreference depending upon the specified dataset.

  • .NET/.NET Core
  • Java
public IDictionary<string, RefreshPreference> GetDatasetsToRefresh(IDictionary<string, object> userContexts)
{
    DateTime? lastRefreshTime;
    bool datasetHasUpdated;

    // Create a dictionary for datasets to refresh with their refresh preference 
    IDictionary<string, RefreshPreference> DatasetsToRefresh = new Dictionary<string, RefreshPreference>();

    foreach (var dataset in userContexts.Keys)
    {
        switch (dataset.ToLower())
        {
            // If dataset is products, check if dataset has been updated in data source
            // if yes, then refresh the dataset now
            case "products":
                lastRefreshTime = userContexts[dataset] as DateTime?;
                datasetHasUpdated = HasProductDatasetUpdated(dataset, lastRefreshTime);
                if (datasetHasUpdated)
                {
                    DatasetsToRefresh.Add(dataset, RefreshPreference.RefreshNow);
                }
                break;

            // If dataset is suppliers, check if dataset has been updated in data source
            // if yes, then refresh dataset on next time of day
            case "suppliers":
                lastRefreshTime = userContexts[dataset] as DateTime?;
                datasetHasUpdated = HasSupplierDatasetUpdated(dataset, lastRefreshTime);
                if (datasetHasUpdated)
                {
                    DatasetsToRefresh.Add(dataset, RefreshPreference.RefreshOnNextTimeOfDay);
                }
                break;

            default:
                // Invalid dataset
        }               
    }
    // Return the dictionary containing datasets to refresh on polling with their refresh preferences
    return DatasetsToRefresh;
}
public Map<String, RefreshPreference> getDatasetsToRefresh(Map<String, Object> userContext) {
    @Nullable LocalDateTime lastRefreshTime;
    boolean datasetHasUpdated;

    // Create a map for datasets to refresh with their refresh preference
    Map<String, RefreshPreference> datasetsToRefresh = new HashMap<String, RefreshPreference>();

    for (var dataset : userContext.keySet()) {
        switch (dataset.toLowerCase()) {
            // If dataset is products, check if dataset has been updated in data source
            // if yes, then refresh the dataset now
            case "products":
                lastRefreshTime = (LocalDateTime) userContext.get(dataset);
                datasetHasUpdated = hasProductDatasetUpdated(dataset, lastRefreshTime);
                if (datasetHasUpdated) {
                    datasetsToRefresh.put(dataset, RefreshPreference.RefreshNow);
                }
                break;

            // If dataset is suppliers, check if dataset has been updated in data source
            // if yes, then refresh dataset on next time of day
            case "suppliers":
                lastRefreshTime = (LocalDateTime) userContext.get(dataset);
                datasetHasUpdated = hasSupplierDatasetUpdated(dataset, lastRefreshTime);
                if (datasetHasUpdated) {
                    datasetsToRefresh.put(dataset, RefreshPreference.RefreshOnNextTimeOfDay);
                }
                break;

            default:
                // Invalid dataset
        }
    }
    // Return the map containing datasets to refresh on polling with their refresh preferences
    return datasetsToRefresh;
}

Dispose All Resources

In the end, calling the Dispose method ensures that you have closed/deleted all resources.

  • .NET/.NET Core
  • Java
public void Dispose()
{
    // Dispose off all resources like
    connection.Close();
}
public void dispose() {
    // Dispose off all resources like
    connection.close();
}
Note

Configure Cache Loader/Refresher on NCache by referring to Configure Cache Loader and Refresher in Administrator’s Guide for help.

See Also

Components of Cache Startup Loader and Refresher
Implementation of Cache Loader and Refresher
Data Source Providers (Backing Source)
Custom Cache Dependencies

Back to top Copyright © 2017 Alachisoft