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

Implement Cache Loader and Refresher

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 Management Center or Command Line Tools. 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. 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 client implements the ICacheLoader interface to configure its custom Loader and Refresher logic. If you configured two datasets from the NCache Management Center: Products and Suppliers. The following implementation loads Products and Suppliers on cache startup. Additionally, it refreshes these datasets on their specified refresh time interval. For more details on the components of Cache Loader, refer to the chapter Components of Cache Startup Loader and Refresher.

Prerequisites

  • .NET
  • Legacy API
  • To learn about the standard prerequisites required to work with all NCache server-side features, please refer to the given page Server-Side API Prerequisites.
  • For API details, refer to: ICache, CacheItem, ICacheLoader.
  • This should be a class library project.
  • Make sure to configure the Cache Loader using the NCache Management Center or Command Line Tools on the NCache cluster.
  • Install either of the following NuGet packages in your .NET client application:
    • Enterprise: Install-Package Alachisoft.NCache.SDK -Version 4.9.1.0
    • Professional: Install-Package Alachisoft.NCache.Professional.SDK -Version 4.9.1.0
  • 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 following namespaces in your application:
    • Alachisoft.NCache.Runtime.CacheLoader
    • Alachisoft.NCache.Runtime.Caching
    • Alachisoft.NCache.Runtime.Dependencies
  • To learn more about the NCache Legacy API, please download the NCache 4.9 documents available as a .zip file on the Alachisoft Website.

Initialize Cache 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
  • Legacy API
 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);
    }
}
// Using NCache Enterprise 4.9.1

// Initializing data source settings
void ICacheLoader.Init(IDictionary parameters, string cacheId)
{

    IDictionaryEnumerator ide = parameters.GetEnumerator();
    while (ide.MoveNext())
    {
        // Get connection string
        if (ide.Key.ToString().Equals("connectionstring",
        StringComparison.OrdinalIgnoreCase))
        {
            string connString = ide.Value.ToString();
            connection = new SqlConnection(connString);
            connection.Open();
        }

        // Get distribution hints – distributionhint is a keyword
        if (ide.Key.ToString().Equals("distributionhint",
        StringComparison.OrdinalIgnoreCase))
        {
            _hint = ide.Value.ToString();
        }
   }
}
Note

To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.

Load Data on Cache Startup

The following implementation of the LoadDatasetOnStartup method fetches a Product from the 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 the cache.

  • .NET
  • Legacy API
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;
}
// Using NCache Enterprise 4.9.1

// Load data from source into cache

LoaderResult ICacheLoader.LoadNext(object index)
{
    if (_hint != null)
        return LoadData(index);
    else
        return new LoaderResult();
}

protected LoaderResult LoadData(object index)
{
    // Compare hints and load respective data
    if (_hint == "Products")
        return LoadProductData(index);
    else if (_hint == "Customer")
        return LoadCustomerData(index);
    else if (_hint == "Order")
        return LoadOrderData(index);
    else
        return new LoaderResult();
}

protected LoaderResult LoadProductData(object index)
{
    LoaderResult result = new LoaderResult();
        int lastIndex = 0;
        if (index != null)
        {
            lastIndex = (int)index;
        }
        // Fetch specific data based on query
        SqlCommand command = new SqlCommand("SELECT * FROM Products  WHERE ProductID > " +                 
        lastIndex.ToString() + " AND ProductID< " + (lastIndex+10).ToString(), connString);

        SqlDataReader reader = command.ExecuteReader();

        int nextLimit = 50 + lastIndex;
        string key = "Product_"+ product.ProductID;

        while (reader.Read() && lastIndex < nextLimit && lastIndex < ItemsPerHint)
        {
            Product product = new Product();
            product.ProductName = reader["ProductName"].ToString();
            product.ProductID = Convert.ToInt32(reader["ProductID"]);

            ProviderCacheItem providerItem = new ProviderCacheItem(product);
            result.Data.Add(key, providerItem);

            lastIndex++;
        }
        if (lastIndex < ItemsPerHint)
        {
            result.UserContext = lastIndex;
            result.HasMoreData = true;
        }
    return result;
}

protected LoaderResult LoadCustomerData(object index)
{
    // Perform operations
    return new LoaderResult();
}

protected LoaderResult LoadOrderData(object index)
{
    // Perform operations
    return new LoaderResult();
}

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
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;
}

Get Datasets To Refresh

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

  • .NET
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;
}

Dispose All Resources

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

  • .NET
  • Legacy API
public void Dispose()
{
    // Dispose off all resources like
    connection.Close();
}
// Using NCache Enterprise 4.9.1

void Dispose()
{
    // Dispose off all resources 
}
Note

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

Additional Resources

NCache provides a sample application for Cache Loader and Refresher on GitHub.

See Also

.NET: Alachisoft.NCache.Runtime namespace.

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Community
  • 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