Whenever we build large systems, performance is one of the key aspect that we need to take into consideration. The performances can generally correspond to the load balancing of the servers, the response time of the pages we build, so on and so forth! When we work on the force.com platform most of this heavy lifting is taken care by the platform itself. Such a relief for the developers!. However there are certain things that we can tweak around to improve the performance even further, and one such concept is the use of Platform Cache.
What is a Platform Cache
Yes, you read it right. It is "Cache". The same old concept of caching which most of us must have learnt long back during the courses such as Operating Systems. Think of cache as a small amount of memory, which stores the most frequently used tokens. So, when we want to search for something, instead of directly diving and searching into the databases, it is better if we can check in the cache first. If we find the required one, then it is a hit!, and this saves a lot of time !
A similar concept is the Platform Cache, we can allocate a small amount of the org space as Cache, and store the most frequently accessed details for a limited time. For more details on the Cache, please refer to this simple and easy trailhead!
Platform Cache to improve the API callouts.
A general scenario which most of us encounter when consuming an external service is the use of access tokens. If the access tokens are fetched every time we hit the service there will be a significant increase in the number of callouts. On the other hand we can store it in some record of an object, or as custom setting (if the token has an expiry) and thereby reduce some callouts.
The second approach works fine, but there is still a scope for improvement in some use cases. Consider the use case where a single token (of the external system) is being used for all the users in Salesforce.
Instead of storing the token in custom settings or objects, we can store it in the Platform Cache using Apex. Platform cache is much like a Map. It takes in key - value pairs, so we can assign a meaningful key and store the token as the value. Whenever we need to access the token, we simply fetch it from the cache and thereby avoid callouts, or SOQL. The values are retained for 24 hours by default and can be extended to 48 hours in case of the Org Cache. The improvement in terms of the speed might not be visible since apex is pretty pretty fast!! But still at conceptual level this one is better considering the multi tenant system.
The code in Apex would be like this:
Cache.OrgPartition orgPart = Cache.Org.getPartition('local.SomePartionName'); orgPart.put('IntegrationAccessToken','12w12e12wzws1jnds3rbhh3'); orgPart.put('IntegrationTokenExpiry', '10/03/2018'); System.debug((String)orgPart.get('IntegrationAccessToken'));
The flow would be to check the expiry date in cache first, if it is available and is in range, then use the cached value, else go and get the value by making a callout and store it in cache for future usage!
Another useful aspect of using this is, the value will not be visible out rightly on the UI, so that adds another layer of abstraction to the tokens!
Also, there is a concept of Session Cache, that can be used to tie values to individual user sessions!
This way we can ensure improved performance by utilising the Platform Cache as an integration-best-practice.