Platform Cache

P

Hi All,

I would like to share one of the challenges that I faced recently. My requirement was to get a value from  first page to the third page in sequence.
I thought of storing the value in custom setting, but there can be more than one user using the functionality at the same time. So not Done.
Cookies did not work for me. I tried several other concepts and ended up using Platform Cache.

Platform Cache will let us to store and retrieve data associated with salesforce session or throughout org. It has two varients:

1. Session Cache: Stores data for the user session only. Before we proceed with example lets know about the Plaform Cache Partition.

We use Platform Cache partitions to improve the performance of your applications. Partitions allow you to distribute cache space in the way that works best for your applications. Caching data to designated partitions ensures that it’s not overwritten by other applications or less-critical data.
To use Platform Cache, first set up partitions using the Platform Cache Partition tool in Setup. Once you’ve set up partitions, you can add, access, and remove data from them using the Platform Cache Apex API.
To know more on Partitions visit here

Method of using Session Cache:

To store data using default partition.
Cache.Session.put(‘wholeAndSoleKey’,’data’ );

To check if key exists:
Cache.Session.contains(‘wholeAndSoleKey’);

To fetch data:
Cache.Session.get(‘wholeAndSoleKey’);

To remove data:
Cache.Session.remove(‘wholeAndSoleKey’);

Example 1 involving two different users:
Here suppose for user1 started the process and session cache has saved data under key ‘wholeandsolekey’.
Cache.Session.put(‘wholeAndSoleKey’,’dataUser1′);

User2 is now started process and session cache stores value under same key ‘wholeandsolekey’.
Cache.Session.put(‘wholeAndSoleKey’,’dataUser2′);

Now user1 is trying to fetch data from session cache:
Cache.Session.get(‘wholeAndSoleKey’);

User 1will get data as ‘dataUser1’. That means, when user2 saved its data it did not override user1’s data even though they are both using the same key to fetch data.

Example 2 involving only one user:
Here suppose there is a button on Contact object. class having session cache is being executed on click of the click.
User1 clicked button on contact1, session cache stored the value in cache:
Cache.Session.put(‘wholeAndSoleKey’,’contId1′);

User1 clicked the button on record 2:
Cache.Session.put(‘wholeAndSoleKey’,’contId2′);

User1 is now fetching data from session cache and she gets value as contId2. contId1 is overridden by contId2 since same user is using same key to store data.

This is how session cache works.

Will come up with the second varient of Platform cache soon.

Thank you,
Swathi Melkundi

About the author

Swathi Melkundi
By Swathi Melkundi

Category