top of page
Search
Writer's pictureDeepak

All you need to know about Platform Cache

What is Platform Cache? What is the cache in Salesforce?


Cache : The cache (pronounced "cash") is a space in your computer's hard drive and in RAM memory where your browser saves copies of previously visited Web pages. Your browser uses the cache like a short-term memory. Instead of downloading an image from a recently viewed website, it will load the image from your cache folder, thereby making the browsing process a little quicker.


Here’s an analogy : Suppose it 5:00 PM, and you are famished, what are the possible option for you to have food quickly. One, you can go to near by store buy something and eat, it will take its own sweet time. 2nd, you can order something online and wait for food to delivered. 3rd, you can open your refrigerator and take out already stored food and have it quickly. If you access the temporary cache of food in your refrigerator, your food is closer and you get to eat it faster! Also, you accomplish your goal more efficiently. A data cache has similar advantages.


Platform Cache: It is a memory layer that stores Salesforce session and org data for later access.



Why Platform Cache?


When you use platform cache your org run faster and more efficiently as data was already stored in application layer cache memory. Applications can quickly access this data; they don’t need to duplicate calculations and requests to the database on subsequent transactions. You can call platform cache as a RAM for your cloud application.


With Platform cache you give priority to frequently used critical data for easy access from cache memory.


Architecture diagram of Platform Cache




Where can I use Platform Cache?


  • You can use Platform Cache in your code almost anywhere you access the same data again and again. Using cached data, it improves performance of your app and is faster than performing SOQL queries repetitively, making multiple API calls, or computing complex calculations.

  • You can cache data for trigger, class, pages etc.

  • Your cached data can be reused throughout a session, or reused across all users and requests

  • You should cache static data which does not change frequently.

  • You should cache data which is very expensive in querying through SOQL. Check query plan for more information

.


What are the types of Platform Cache?


Org Cache: It stores org-wide data that anyone in the org can use. Org cache is accessible across sessions, requests, and org users and profiles.


Session Cache: Session cache stores data for an individual user and is tied to that user’s session. The maximum life of a session is 8 hours.


Cache allocations by edition

Platform cache comes with platform partition and it let you allocate memory for the above two.


Cache Partitions: Session and org cache allocations can be zero, 5 MB, or greater, and must be whole numbers. The minimum size of a partition, including its org and session cache allocations, is 5 MB.


Default Partition: You can have only one default partition. The default partition enables you to use shorthand syntax to perform cache operations on that partition. This means that you don’t have to fully qualify the key name with the namespace and partition name when adding a key-value pair.



How to use platform cache


To use Platform Cache, first set up at least one partition. Once you’ve set up partitions, you can add, access, and remove data from them using the Platform Cache Apex API.


Store and retrieve data in org cache: Either use theCache.Orgclass methods, or use theCache.OrgPartitionclass to reference a specific partition. Then call the cache methods on that partition


/*1. Create a partition called CurrencyCache.
  2. Each cache key has the following format: Namespace.Partition.Key
  3.Create a key namedDollarToEuroRate.
*/
// Get partition 
Cache.OrgPartition orgPart = Cache.Org.getPartition('local.CurrencyCache');

/*Add cache value to the partition. Usually, the value is obtained from a callout, but hardcoding it in this example for simplicity.*/

orgPart.put('DollarToEuroRate', '0.91');

// Retrieve cache value from the partition

String cachedRate = (String)orgPart.get('DollarToEuroRate');


Do cached values last forever?


Cached data is not always guaranteed. Platform Cache is intended as a temporary space. For example, the cache might have expired. Even if the cache is still alive, it is possible that your cached data might be evicted from the cache. Just like taking some non food from freeze to make space for perishable items.


For session cache, your data can live up to 8 hours in the cache. For org cache, your data can live up to 48 hours in the cache. By default, the time-to-live value for org cache is 24 hours.


Session cache expires when its specified time-to-live value is reached or when the user session expires, whichever comes first. Org cache expires when its specified time-to-live value is reached.



Best practice for handling cache misses


Make sure you check your get call request with not null condition to make sure you don't fall in a trap. Here is the code snippet to check.




Cache.OrgPartition orgPart =Cache.Org.getPartition('local.CurrencyCache');String cachedRate =(String)orgPart.get('DollarToEuroRate');

// Check the cache value that the get() call returned.

if(cachedRate !=null){
    // Display this exchange rate   
}else{
        // We have a cache miss, so fetch the value from the source.
        // Call an API to get the exchange rate.
}

Store and retrieve data in Session Cache:



// Get partition

Cache.SessionPartition sessionPart =Cache.Session.getPartition('local.CurrencyCache');

// Add cache value to the partition sessionPart.put('FavoriteCurrency','JPY');

// Retrieve cache value from the partitionString 

cachedRate =(String)sessionPart.get('FavoriteCurrency');



Benefits of using Platform cache





Hit like & share button if you really gained something from this post.

Happy Learning :)


Reference : Salesforce blogs and trailhead.


103 views0 comments

Recent Posts

See All

Comments


bottom of page