Data caching

When you cache data in memory, you do not need to retrieve the same data from the database each time you need to use it. This can lead to considerably better performance as it cuts down the database round trip required to retrieve that data. Having said that, you shouldn't go around caching everything you use. For example, short lists of data that don't change frequently would benefit more from caching compared to large (for example, 100 records and above) amounts of data.

The following are general guidelines to follow in selecting suitable candidates for data caching:

• Data that is not expected to change frequently

• Data that is used repeatedly throughout the application (for example, data in drop-down lists)

Now let's take a look at how you can apply data caching to the sales force application. If you recall, you've created the Products table, which contains the full list of products in your application. This same list is retrieved from the database each time you view the details of an existing lead, and is displayed in the Interest tab of the Account Details window. As the list of products is seldom expected to change once it has been synced down from the server, we can cache it in memory to improve performance.

All you need to do to cache the list of products in memory is the following:

1. Retrieve the full list of products from the database into a ProductCollection object when viewing account details for the first time.

2. Store this ProductionCollection object in a global variable.

3. On all subsequent requests to view account details, retrieve the list of products from this cached object instead of the database.

Let's make some changes to your code. Declare a public static ProductCollection variable named CachedProducts to the GlobalArea class in your SalesForceApp project (as highlighted in the following code snippet).

public class GlobalArea {

private static PluginManager _PluginManager = new

PluginManager(); private static Application _Application = new

Application(); private static CustomErrorHandler _ErrorHandler = new CustomErrorHandler("\\My

Documents\\Personal\\error.log"); private static ProductCollection _CachedProducts = null;

public static ProductCollection CachedProducts {

return _CachedProducts;

_CachedProducts = value;

Now let's make some changes to the function that retrieves the list of products from the database. This is the GetProductList() function in the Application.cs file. To see the difference in terms of performance, you can also use the PerfTimer class you've created earlier to display the time elapsed in retrieving the list of products. All these changes are highlighted in the following code snippet:

public ProductCollection GetProductList() {

DataSet _productsDataset; ProductCollection _products; PerfTimer _timer = new PerfTimer(); _timer.StartTimer();

if (GlobalArea.CachedProducts == null) {

_productsDataset =

GlobalArea.PluginManager.GetActivePlugin. GetProductList(); _products = new ProductCollection

(_productsDataset.Tables["Products"]);

_products = GlobalArea.CachedProducts;

MessageBox.Show("Loading the product list took :" + _timer.StopTimer() + " milliseconds");

return _products;

If you now run the sales force application and try to view an existing account or create a new account, you will see the following pop up. As this is the first time you are accessing this screen, the cache has not yet been loaded and so, a round trip is made to the database to retrieve the list of products. After that, this list is then stored in the cache.

If you close the Account Details window and reopen it, you will see that the load time has now been reduced to a mere 19 seconds (only 10 percent of the original time taken!). This is because it is now retrieving the list of products from the cache.

SalesForceApp

Loading the product list took : 19 milliseconds

If you use these types of lists frequently in your application, you can realize huge performance cost savings by caching these lists in the same preceding fashion.

Was this article helpful?

0 0

Post a comment