How to Use Cache Startup Loader?

It is sometimes important that cache is preloaded with some base data before a user starts using it. NCache cache startup loader feature allows clients to implement an interface that is called /invoked as soon as cache starts up and you can pre-populate cache with desired data. User can load some important domain objects, framework and application configurations, resource files etc.

Here are details on cache startup loader implementation:


For enabling Cache Startup Loader, your program needs a reference following namespaces.

using Alachisoft.NCache.Runtime.CacheLoader;
using Alachisoft.NCache.Runtime.Caching;

ICacheLoader Interface:

Class implementing interface Alachisoft.NCache.Runtime.CacheLoader.ICacheLoader allows framework to load data from the master datasource in the cache on cache startup. Here are details on interface implementation.

public void Init(System.Collections.IDictionary parameters);
public bool LoadNext(ref System.Collections.Specialized.OrderedDictionary 
data, ref object index); public void Dispose();

Implementing ICacheLoader

Following is a list of methods needs to be defined by the class implementing interface and a brief description of their purpose.

void Init(IDictionary parameters);

This method performs tasks like allocating resources, acquiring connections etc. When the cache is initialized and Cache Startup Loader is enabled, it callsInit method to notify the client that cache has initialized and you may initialize your data source too. The parameters passed as an argument contains all the parameters (if any) that were specified using NCache Manager cache/cluster views. These parameters can be utilized in many ways. For example, connection string of a datasource can be specified as a parameter through NCache Manager. Whenever cache will be initialized, the connection to the datasource could be made using this connection string passed as an argument. Thus, it provides a flexible way to change the datasource dynamically without requiring code changes.

void Dispose();

This method performs tasks like releasing the resources etc. When Cache is stopped or LoadNext returns false, it calls the Dispose() method to notify the client that the task has completed or cache has stopped now and you can free the resources related to your datasource.

public bool LoadNext(ref System.Collections.Specialized.OrderedDictionary data, 
ref object index);

This method should contain logic to load object(s) from the master datasource. After Init call, framework calls LoadNext method in a loop till LoadNext returns false. This method receives an OrderedDictionary reference that user needs to set and populate with its respective data. Another ref variable is index. User must set it to be starting index in given OrderedDictionary. This function allows user to add data in chunks, or add it as a single operation. If user wants to add data in chunks, he must return True back to caller framework, indicating that this method should be called again. User should return false, to end Startup loading of data by the framework.

Using ICacheLoader:

Here is how your class should look like for ICacheLoader:

namespace CacheLoaderApp
    class CacheLoader : ICacheLoader
        private string _connString = @"Server=stagingServer2; initial 
catalog=Northwind; User ID=sa; password=xxxxxx;";
        private SqlConnection _connection;
        #region ICacheLoader Members
        public void Init(System.Collections.IDictionary parameters)
            _connection = new SqlConnection(_connString);
        public bool LoadNext(ref System.Collections.Specialized.OrderedDictionary 
data, ref object index)
            int nextIndex = 0;
            if (index != null)
                nextIndex = (int)index;
            SqlCommand command = new SqlCommand("SELECT * FROM Customers WHERE CustomerID > " + 
nextIndex.ToString() + " AND CustomerID < " + (nextIndex + 10).ToString());
            SqlDataReader reader = command.ExecuteReader();
            if (!reader.HasRows)
                return true;
            while (reader.Read())
                Customer customer = new Customer();
                customer.CustomerID = reader["CustomerID"].ToString();
                customer.ContactName = reader["ContactName"].ToString();
                customer.Country = reader["Country"].ToString();
                ProviderCacheItem provideritem = new ProviderCacheItem(customer);
                provideritem.Dependency = new KeyDependency("customer:1");
                provideritem.AbsoluteExpiration = System.DateTime.Now.AddSeconds(30);
                data.Add("Customer:" + customer.CustomerID, provideritem);
            index = nextIndex + 10;
            return false;
        public void Dispose()
            if (_connection != null)

Note: NCache logs the warnings in Application event log in case of an exception during loading the assemblies.

Configuring Startup Loader in NCache Manager:

To enable Cache Startup Loader, you need to implement ICacheLoader and specify it in the cache properties. NCache Manager provides an interface for specifying it under the cache properties. For Cache Startup Loader, follow these steps:

  1. From the start programs, launch NCache Manager.
  2. Open the project containing cache or cluster for which you want to configure the Cache Startup Loader.
  3. From the explorer, select the cache or cluster. In the right pane, cluster/cache view will be opened.
  4. From the view, select the Cache Loader tab.
  5. Check the Enable Cache-Startup Loader check box.
  6. Click Browse button and browse the Assembly that contains the class implementing ICacheLoader and then select class name from the list. The Assembly implementing ICacheLoader must contain the reference of Alachisoft.NCache.Runtime.dll.
  7. You can provide some additional parameters (like chunkSize etc.) if required.
  8. You can specify number of retries if any cache operation fails.
  9. You can also specify retry interval between each retry. This value is in sec's.
  10. Deploy the appropriate assemblies to the cache servers using the Deploy Cache Loader button on a same tab view.


What to Do Next?


Signup for monthly email newsletter to get latest updates.

Contact Us


+1 (214) 764-6933 (US)

+44 20 7993 8327 (UK)

© Copyright Alachisoft 2002 - . All rights reserved.