In a distributed cache environment, it’s often crucial for certain data to be readily available in the cache as soon as the application begins execution. Typically, when a cache starts, it is empty, leading to expensive trips to the data source to fetch necessary data, which negatively impacts application performance. To address this, it’s important to preload the cache with essential reference data before the application starts using it.
Fortunately, NCache offers a Cache Startup Loader feature that allows users to implement an interface for preloading specific reference data into the cache at startup. This ensures that the application experiences fewer cache misses and minimizes trips to the data source, resulting in improved performance.
Properties of Cache Startup Loader
Every time the cache starts up, the Cache Startup Loader automatically loads data from the data source for the application to use beforehand. Therefore, before moving onto how the Cache Loader works, it’s important to understand some of its key properties:
- NCache Execution Service: Manages the loading of data from the data source into the cache at startup.
- Datasets: Allows users to group different types of data so that they can load or refresh them separately at different intervals or events to achieve parallelism.
- Dataset Assignment: Distributes datasets to nodes in a round-robin fashion to ensure efficient data loading and maximum utilization.
- Loading Mechanism: Defines which objects to load from the master data source into the cache as CacheItems during startup.
- Cache Loader Retries: Configures the number of retries for failed data loading operations.
- Cache Loader Retry Interval: Sets the time interval to wait before retrying the failed Cache Loader operations.
- On-Demand Dataset Refresh: Allows manual refreshing of datasets through the Invoke-RefresherDataset cmdlet, providing flexibility to update data.
How to Implement Cache Startup Loader?
To enable Cache Startup Loader, the user needs to implement the ICacheLoader interface. NCache uses this interface with the methods explained below to load data from the data source in the cache-on-cache startup.
Initialize Cache Startup Loader
To initialize the Cache Startup Loader, the Init method is called on the Cache Startup to pass the configured parameters so that the user can use it accordingly to initialize their cache and the data source. To do so, just create the connection by opening a database connection like SQL and initialize a cache with the given name like the below sample implementation for .NET and Java.
1 2 3 4 5 6 7 8 9 |
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); } } |
1 2 3 4 5 6 7 8 |
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); } } |
Load Data on Cache Startup
After the successful initialization of the Cache Startup Loader, comes the LoadDatasetOnStartup method of the ICacheLoader interface. It fetches the data from the data source and adds it into the cache upon startup. It returns a user context that holds the information about the data loaded in the cache.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
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 product 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; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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 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; } |
Preloading data into the cache is essential for boosting performance, but over time, this data can become outdated if the underlying data source changes. To keep the cached data fresh and accurate, NCache provides the Cache Refresher feature. This feature works alongside the Cache Loader to regularly update the cached data, ensuring it stays synchronized with the data source.
To learn more about the Cache Refresher, refer to the Cache Refresher documentation.
Configure Cache Loader through the NCache Management Center
Once the user has implemented the Cache Startup Loader, they can configure it through the NCache Management Center. Below is how it can be done.
Conclusion
NCache’s Cache Startup Loader feature offers a robust solution for preloading data into your cache, ensuring that your application starts with all the necessary information readily available. This automation minimizes manual tasks and minimizes database trips, leading to faster application performance and enhanced scalability. By leveraging NCache’s capabilities, you can significantly boost your application’s efficiency and responsiveness, making it well-equipped to handle high-demand scenarios and dynamic data requirements.