• Products
  • Solutions
  • Customers
  • Resources
  • Company
  • Pricing
  • Download
Try Playground
  • Server-side API Programming
  • Loader and Refresher
  • Implement Loader and Refresher
Show / Hide Table of Contents
  • Programmer's Guide
  • Setting Up Development Environment
    • .NET
      • Client API Prerequisites
      • Server-side API Prerequisites
    • Java
      • Client API Prerequisites
      • Server-side API Prerequisites
    • Python
      • Client API Prerequisites
    • Node.js
      • Client API Prerequisites
  • Client Side API Programming
    • Error Handling
    • Troubleshooting
    • Cache Keys and Data
    • How to Connect to Cache
    • Basic Operations - An Overview
      • Add Data
      • Update/Insert Data
      • Retrieve Data
      • Remove Data
    • Groups
      • Overview
      • Add/Update Data with Groups
      • Retrieve Data with Groups
      • Remove Data with Group
      • Search Group Data Using SQL
      • Delete Group Data Using SQL
    • Tags
      • Overview
      • Add/Update Data with Tags
      • Retrieve Data with Tags
      • Remove Data with Tags
      • Search Tag Data Using SQL
      • Delete Tag Data Using SQL
    • Named Tags
      • Overview
      • Add/Update Data with Named Tags
      • Remove Data with Named Tags
      • Search Data with Named Tags Using SQL
      • Delete Data with Named Tags Using SQL
    • Expirations
      • Overview
      • Absolute Expiration
      • Sliding Expiration
    • Data Dependency
      • Key Dependency
      • Multi-Cache Dependency
    • Dependency on Database
      • SQL Server
      • Oracle
      • OleDB with Polling
      • CLR Procedures in SQL Server
    • Dependency on External Source
      • File Dependency
      • Custom Dependency
      • Aggregate Dependency
    • Locks
      • Types of Locking
      • Pessimistic Locking
      • Optimistic Locking
    • SQL Query
      • Overview
      • Define Indexes Programmatically
      • Query with ExecuteReader and ExecuteScalar
      • Delete Data with ExecuteNonQuery
      • SQL Reference
    • LINQ Query
      • Overview
      • LINQ Query for Objects
      • LINQ Reference
    • Data Structures
      • Overview
      • List
      • Queue
      • Set
      • Dictionary
      • Counter
      • Invalidation Attributes
      • Searchable Attributes
      • Query on Data Structures
      • Remove from Data Structure
    • Events
      • Cache Level Events
      • Item Level Events
      • Management Level Events
    • Pub/Sub Messaging
      • Overview
      • Topics
      • Publish Messages
      • Subscribe to a Topic
      • Pub/Sub Events
    • Continuous Query
      • Overview
      • Use Continuous Query
    • Stream Processing
      • Add/Update Stream Data
      • Retrieve Stream Data
    • JSON
      • Overview
      • Use JSON Objects
      • Query JSON Data
    • Security API
      • Login with Credentials
    • Management API
    • Clear Cache
    • Error Logging
    • Location Affinity
  • Server-side API Programming
    • Loader and Refresher
      • Overview
      • Implement Loader and Refresher
      • Components of Loader/Refresher
    • Data Source Providers
      • Read-through
        • Implement Read-through
        • Use Read-through
      • Write-through
        • Implement Write-through
        • Use Write-through
        • Use Write-behind
    • Custom Dependency
      • Implement Extensible Dependency
      • Implement Bulk Extensible Dependency
      • Implement Notify Extensible Dependency
    • Bridge Conflict Resolver
    • Entry Processor
      • Overview
      • Implement Entry Processor
    • MapReduce
      • Overview
      • Implement MapReduce
      • Use MapReduce
    • MapReduce Aggregator
      • Overview
      • Implement and Use Aggregator
    • Compact Serialization
  • Client Side Features
    • ASP.NET Core Caching
      • Session Storage
        • Session Provider
        • IDistributedCache
        • Sessions Usage
        • Multi-site Session Provider
        • Session Sharing with ASP.NET
      • SignalR
        • NCache Extension for SignalR Core
      • Response Caching
        • Configure and Use
        • Configure with IDistributedCache
      • Data Caching
        • NCache API
        • IDistributedCache API
      • Data Protection Provider
        • Configure
    • Java Web App Caching
      • Web Sessions
        • Overview
        • Configure App
          • Add Maven Dependencies
          • Deploy Application
        • Multi-site Sessions
    • Node.js App Caching
      • Web Sessions
    • ASP.NET Caching Benefits and Overview
      • ASP.NET Session State Provider Properties
      • Multi-region ASP.NET Session State Provider Configuration
      • Session Sharing between ASP.NET and ASP.NET Core
      • ASP.NET SignalR Backplane
        • NCache Extension for SignalR
      • ASP.NET View State Caching
        • View State Content Optimization Configuration
        • Group View State with Sessions
        • Limit View State Caching
        • Page Level Grouping
      • ASP.NET Output Cache
        • Output Caching Provider Overview
        • Output Cache with Custom Hooks
  • .NET Third Party Integrations
    • Entity Framework (EF) Core
      • Installation
      • Configure
      • EF Core Extension Methods
        • Extension Methods
        • Cache Handle
        • Caching Options
        • Query Deferred API
      • Logging in EF Core
    • Entity Framework EF 6
      • EF Second Level Cache
      • EF Caching Resync Provider
      • EF Caching Configuration File
    • NHibernate
      • Second Level Cache
      • Query Caching
      • Synchronize Database with Cache
    • Debug NCache Providers in Visual Studio
  • Java Third Party Integrations
    • Hibernate
      • Second Level Cache
      • Configure Cacheable Objects and Regions
      • Configure Application
      • Query Caching
    • Spring
      • Overview
      • Use NCache with Spring
        • Configure Generic Provider
        • Configure JCache Spring Caching Provider
        • Configure Caching Declaration
        • Configure Spring Sessions
    • JCache API
      • CRUD Operations
      • Expiration
      • Events
  • Third-Party Wrappers
    • AppFabric to NCache Migration
      • AppFabric API vs. NCache API
      • Configuration Differences Between AppFabric and NCache
      • Migrating from an AppFabric Application to NCache
    • Redis to NCache Migration
      • Redis to NCache Migration
    • Memcached Wrapper
      • Memcached Gateway Approach
      • Memcached Client Plugin for .NET

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.

Note

The Cache Loader and Refresher are [Deprecated] for NCache Java.

Cache Loader and Refresher Prerequisites

  • .NET
  • Java
  • 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.
  • 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: Cache, CacheItem, CacheLoader.
  • 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.
  • 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.CacheLoader and Alachisoft.NCache.Runtime.Caching namespaces in your application.

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

Load Data on Cache Startup (Legacy API)

The following is the implementation for loading cache data on startup in the Legacy API:

  • .NET
//load data from source into cache
LoaderResult LoadNext(object index)
{
    LoaderResult result = new LoaderResult();

    int nextIndex = 0;
    if (index != null)
        nextIndex = (int)index;

    for (; nextIndex < _totalCount; nextIndex++)
    {
        Customer customer = new Customer(nextIndex, "name:" + nextIndex.ToString());
        ProviderCacheItem cacheItem = new ProviderCacheItem(customer);
        if (nextIndex < DependencyCount)
        {
            result.HasKeyDependency = false; //add in bulk
        }
        else if (nextIndex < DependencyCount * 2 && nextIndex > DependencyCount)
        {
            result.HasKeyDependency = true; //add sequentially
            cacheItem.Dependency = new KeyDependency("Customer: 0");
        }
        result.Data.Add("Customer: " + nextIndex, cacheItem);
    }
    result.HasMoreData = true;
    result.UserContext = nextIndex;

    return result;
}

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
  • 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 the RefreshPreference. This method takes the user context and assigns a RefreshPreference depending upon the specified dataset.

  • .NET
  • 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 to prevent wastage.

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

Additional Resources

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

See Also

.NET: Alachisoft.NCache.Runtime namespace.
Java: com.alachisoft.ncache.runtime.cacheloader namespace.

In This Article
  • Cache Loader and Refresher Prerequisites
  • Initialize Cache Loader and Refresher
  • Load Data on Cache Startup
  • Load Data on Cache Startup (Legacy API)
  • Refresh Dataset
  • Get Datasets To Refresh
  • Dispose All Resources
  • 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