Cache Loader and Refresher in NCache

NCache is an extremely fast and scalable In-Memory Distributed Cache for .NET and caches application data to reduce expensive database trips. Use NCache to remove performance bottlenecks related to your data storage and database.

As your cache size grows and you are increasingly caching reference / lookup data, you face the issue of needing to fill up an empty cache upon startup. Then there is the issue of keeping this cached data fresh and also adding more data to it on a regular basis in order to keep entire datasets in the cache. And, to do all of this, you not only have to develop a custom application but also host it somewhere and make sure it always runs upon cache startup and also on scheduled intervals.

This is a lot of extra management and headache that NCache takes over for you through its powerful Cache Loader / Refresher feature. NCache lets you develop custom server-side code and register it for Cache Loader and Refresher.

NCache provides the following to you:

  1. Cache Loader (Distributed & Parallel): you can develop a Cache Loader as server-side code that gets deployed and runs on all cache servers in the cluster. Then, when the cache starts, NCache calls this Cache Loader on each cache server in parallel to go and read its portion of the data (called datasets) from your database / data source and load it to the cache. Cache Loader runs in parallel on all cache servers to distribute the work of loading data into multiple parallel datasets.
  2. Cache Refresher (Distributed & Parallel): you can develop a Cache Refresher as server-side code that gets deployed and runs on all cache servers in the cluster. Then, NCache calls your Cache Refresher on regular basis (schedule) to go and get updates or fresh data from your database / data source and put it in the cache. You can also call Cache Refresher on-demand. Cache Refresher runs in parallel on all cache servers to distribute the work of loading data into multiple parallel datasets.
Cache Loader and Refresher in NCache

Benefits of Cache Loader / Refresher

Here are some benefits for using Cache Loader / Refresher capability in NCache.

  1. Parallel Loading / Refreshing of Datasets: if your cache is large, loading it or refreshing it can take some time. And, if you write code for this purpose yourself, you will most likely run it from a single computer and therefore it will load all data sequentially. But, NCache distributes and runs Cache Loader / Refresher on all cache servers. As a result, data loading is parallel and much faster.
  2. High Availability of Cache Loader / Refresher: NCache deploys Cache Loader / Refresher on all cache servers. This means if any cache server goes down, the loading / refreshing does not stop because that is running on other cache servers. And, as you add a new cache server to the cluster, NCache makes Cache Loader / Refresher available on it as well thereby adding to the high availability. This is something you cannot achieve if you wrote this code and ran yourself.
  3. NCache Hosts it for You: if you had to develop and run this code yourself, you would need to make sure it always runs even if the server on which you are running that code goes down. This is because your cache depends on this code to stay fresh and complete. And, this is a lot of work to manage such a thing in production. Fortunately, NCache takes over this responsibility for you and hosts Cache Loader / Refresher for you on all cache servers. NCache also makes sure this code runs on multiple servers for high availability as explained above.
  4. Loader / Refresher Runs Very Close to Cache: as you know, NCache deploys and runs Cache Loader / Refresher on all cache servers in the cluster. This means your cache loader / refresher code is running very close to the cache itself. And, this also speeds up the loading / refreshing of data in case of large datasets.

Feature Highlights

Here are some highlights of Read-through, Write-through, and Write-behind capability in NCache.

  1. Server-side Code (.NET): you develop your Cache Loader / Refresher in .NET. There is an interface for Loader and Refresher that you develop and then deploy your code to all cache servers in the cluster. NCache is then able to call them upon Cache Startup or later for Refresher. Your code is developed in .NET and is called natively by NCache since it is also developed in .NET.
  2. Loader / Refresher Runs in Separate Process: NCache runs your Cache Loader / Refresher in a separate process on each cache server than the process which has the cache data. This means your cache is unaffected by any errors or crashes in your Cache Loader / Refresher code as its process can easily be restarted by NCache Management Service on the same cache server.
  3. Multiple Named Datasets: you can define multiple named datasets to break down your data loading / refreshing into parallel data chunks. By naming the dataset in the configuration, your Cache Loader / Refresher code responsible for loading / refreshing any named dataset knows which dataset it is and then can use the code appropriate to only fetch that data.
  4. Datasets Distributed to Cache Servers at Runtime: NCache uses a round-robin algorithm to distribute datasets to all cache servers in the cluster to load / refresh in parallel. This speeds up loading / refreshing quite a bit since you always have at least 2 cache servers in the cluster and most likely more than 2.
  5. Dataset Level Scheduling (Monthly, Weekly, Daily Time, Daily Interval): NCache lets you define loading / refreshing schedule at Dataset level so each Dataset can have its own schedule. The scheduling can be Monthly, Weekly, Daily Time, and Daily Interval. Daily Time means a specify time of the day every day whereas Daily Interval means running multiple times in a day every day but after waiting for a specific interval from last execution.
  6. On-Demand Dataset Refresh: in addition to providing Dataset level scheduling, NCache also provides a PowerShell Cmdlet that allows you to invoke a specific Dataset refresh on-demand. This way, if you feel any Dataset data has changed but its scheduled time hasn’t come, you can call NCache to refresh it. You can integrate this call in any of your applications where you have a better understanding about the state of your data in your database / data source your application can make the call of whether to refresh on-demand or not.

Cache Loader / Refresher Interface

It is quite simple to develop Cache Loader / Refresher code. Below is an example of interface for it.

Cache Loader / Refresher Interface

public interface ICacheLoader : IDisposable {
	void Init(IDictionary<string, string> parameters, string cacheName);
	object LoadDatasetOnStartup(string dataset);
	object RefreshDataset(string dataset, object userContext);
	IDictionary<string, RefreshPreference> 
GetDatasetsToRefresh(IDictionary<string, object> userContexts);
}
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.