Salesforce Applications frequently need few static data while performing transactions. These data are completely static or updated very less in the application. We should store these data in memory for easy retrieval while performing apex transactions. This post will explain how to enhance apex performance with platform caching and other approaches.
Data that need Caching
Based on the type of application we can cache different information. Below are a few pieces of information that we can cache. These can vary based on the domain we are working on.
- Products information
- Country Information
- Authentication Token
- User Information
Options for Caching
We can utilize the below Salesforce features to store frequent data
- Custom Setting
- Custom Metadata Types
- Static object
- Platform Cache
Both Custom Setting and Custom Metadata types are internally caching data after the first data retrieval. These are mostly used to store application configuration information which is structured data. We can not cache runtime data which is created at runtime. Both are used for permanent cache. We are not sure what kind of data will be cached in memory while running the application so both can not be used for caching such kind of data.
Static variable can also be used for cache items for a transaction but we have to declare lot of variable for specific type to store runtime data. Cached data will be used in only specific transaction and can not be used by other users.
Platform Cache can be used to store any type of data at runtime and it can be used for specific session or by all users. This will be perfect to store data for our use cases.
Let us explore platform cache and its usage.
Platform Cache
The platform cache is temporary cache to store session or org specific data in Salesforce Apex. These data can be stored/retrieved for one user or for multiple users.
Types of Platform Cache
- Session Cache : This will be used to store data which is used for specific session of user. Cached data will be used only for current session, if user logged out from application, data will be removed from Cache. We can store user information or user specific auth token using session cache.
- Org Cache : This can be used to store data which can be used by all application user. Product information, country information can be cached in Org cache as it can be used by all users.
Storing/Retrieving Data in Cache
To store data in session or org cache we have to first create partition. Refer Platform Cache Partitions to create partition. Partitions allow us to distribute cache space based on our business use case. Let us assume we have created partition myPartition to store cache items.
Session Caching
// Add a value to the cache
string currentClient= [Select id,Client__c from User where Id=:UserInfo.getUserId()].Client__c;
Cache.Session.put('local.myPartition.client', currentClient);
//Retrieving cached client
string currentClient= (string)Cache.Session.get('client');
Org Caching
// Add a value to the cache
List<Product2> prds = [Select id,Name,Price from Product2];
Cache.Org.put('local.myPartition.products', prds);
//Retrieving cached products
List<Product2> prds= (List<Product2>)Cache.Org.get('products');
Performance Improvement
Let us take example, our application is showing product information on lightning component. We can store that information in cache as all user will fetch products to add into cart.
//Retrieving cached products
List<Product2> prds=(List<Product2>)Cache.Org.get('products');
if(prds.isEmpty())
{
// Add a value to the cache
prds = [Select id,Name,Price,Price,Quantity,Description from Product2];
Cache.Org.put('local.myPartition.products', prds);
}
Above code will try to retrieve product information from Org Cache and If not found in cache then it will fetch information from database and store into cache.
Below tables are showing loading time for 49,680 products with and without using platform cache.
Without platform Cache
Page Load | Time required to load data (milliseconds) |
---|---|
1st time | 809 ms |
2nd time | 798 ms |
3rd time | 784 ms |
Using plateform Cache
Page Load | Time required to load data (milliseconds) |
---|---|
1st time | 804 ms |
2nd time | 2 ms |
3rd time | 1 ms |
We can see the difference between using Platform Cache in application development. It will load spontaneously using the platform cache after the first load.
Best Practise for using platform Cache
- Always put code to fetch data when no data exist in the cache.
- Try to put a list instead of multiple individual item in the cache
- Clear cache when cached item updated/removed
- The size of individual cached items is limited to 100 KB so put item based on this limitation
References:
Related Posts:
Apex Trigger Code Optimization
Optimize Code by Disabling Debug Mode
Optimizing Salesforce Apex Code
1 comment
[…] Use Platform Caching […]