Preload Data on Cache Startup and Keep it Fresh

When you are using NCache for AppData caching, then most common option is to use Cache Aside approach where data naturally gets added. Initially, this slows down the process, when you have no data in the cache. As a result, a lot of requests go to the database until you have the cache filled up. To avoid this slowness due to lack of data in the cache, NCache provides Cache Startup Loader feature which runs each time you start your cache and fills up the needed data (working set) in the cache from the database.

Similarly, there can be a situation where you have new data being added or getting updated in the database. In order to avoid stale data in the cache, you need to refresh the already loaded data in the cache either on demand or based on some schedule. Therefore, NCache gives Cache Refresher feature to refresh cache data and keep it synced with the database.

How to Implement Cache Startup Loader and Refresher?

Now to implement the Cache Startup Loader and Refresher feature and get to know how a cache can be preloaded with data on cache startup instead of manual addition, follow the steps given below.

Implement ICacheLoader Interface

  • Implement the ICacheLoader interface with its methods to load data from the database in the cache on cache startup.
  • Then, implement Init method that takes a set of parameters which are responsible for initializing the cache and database connections.
  • After that, implement LoadDatasetOnStartup method of ICacheLoader interface which fetches the data from the database and adds it into the cache on cache startup to preload the cache.
  • Finally, implement the RefreshDataset method to refresh the data that has been loaded in the cache either on demand or based on some schedule.

Configure Cache Loader and Refresher

You can configure Cache Startup Loader and Refresher in the following two ways:

  • Using NCache Web Manager, the user can configure Cache Startup Loader and Refresher both.
  • Using PowerShell, users can add or remove the Cache Startup Loader respectively.

Invoke Cache Refresher

  • Using Refresh Interval, a thread runs in the background to check which datasets are ready to be refreshed according to their respective scheduled time.
  • Using, Invoke-RefreshDataset cmdlet, the dataset is refreshed on demand. The RefreshPreference option refreshes dataset either immediately or with certain delay.

Verify Data in Cache on Startup

Using NCache Web Manager you can observe the cache count and with PowerShell, you can execute the following command to check if data has been added in the cache on startup.

Get-CacheCount –Name demoCache

What to Do Next?

Signup for monthly email newsletter to get latest updates.

© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.