Platform Cache in Salesforce

Shama Gurav
5 min readNov 21, 2024

--

Cache in software development is high speed data storage layer which can be used to store data or part of data so that it can be accessed faster. This allows faster processing of future request for most frequently required data. In nutshell, cache can be used to store subset of data which is needed at multiple stages of single or multiple transaction at session level or application level, so that it dose not need to be fetched multiple times from primary source.

Usage of cache results in faster processing and reduce number of callbacks to primary source of the data.

As Salesforce is multi-tenant environment, Salesforce has its own lightning platform cache layer. It provides faster performance and reliability when caching Salesforce session and org data.

Salesforce developer has to deal with number of governor limits. Hence, salesforce developers can certainly benefit from platform cache and take advantage of faster computation as well as reduce the number of SOQL statement being used to fetch the data from salesforce which will lead to less usage of SOQL and CPU time limit.

Salesforce provides two types of platform cache as below,

Org Cache: Data stored in org cache,

  • Can be used by anyone in salesforce org.
  • Can be accessed across sessions, requests, org users and profiles.

Session Cache: Data stored in session cache is tied to logged in user’s session. Hence, it can only be accessed in current user session.

Cache Partitions

Partitions allows you to define cache space.

To use platform cache in salesforce, you must create partitions and define cache capacity required for your app. Each partition consist of org cache and session cache.

Minimum size of partition is 5MB. you can define size of org cache and session cache in partition. For example, Session or Org cache capacity should be integer/whole number. hence it can be between 0 MB to total size of partition.

For example,

  • Org cache: 0MB , Session cache: 5MB
  • Session cache: 0MB , Org cache: 5MB
  • Org cache 2 MB and session cache 3MB.

Partitions let you allocate cache space to balance usage and performance across apps. Caching data to designated partitions ensures that the cache space isn’t overwritten by other apps or by less critical data.

Before deciding to use platform cache, it is wise to review its considerations as below,

  • Cache misses: You can write custom logic to check cache misses or use CacheBuilder Interface.
  • Data loss is possible.
  • Data is not encrypted in cache
  • The session cache can store values up to eight hours. The org cache can store values up to 48 hours.
  • Concurrent read/writes across multiple Apex transactions.
  • Partitions must adhere to the limits within Salesforce.

Platform cache limits:

  1. Maximum Key size limit is 50 character.
  2. Amount of platform cache available varies depending on the type of org.
  • Enterprise org has 10 MB
  • Unlimited and Performance have 30 MB
  • All other editions have 0 MB

3. Minimum partition limit is 1 MB.

4. Maximum size of a single cached item (for put() methods) is 100 KB

5. Minimum developer-assigned time-to-live is 300 seconds (5 minutes)

6. Org cache limits:

  • Maximum local cache size for a partition, per-request is 11,000 KB
  • Maximum developer-assigned time-to-live172,800 seconds is (48 hours)
  • Default org cache time-to-live is 86,400 seconds (24 hours)

7. Session Cache Limits:

  • Maximum local cache size for a partition, per-request 1500 KB
  • Maximum developer-assigned time-to-live is 28,800 seconds (8 hours)
  • Maximum session cache time-to-live is 28,800 seconds (8 hours)

Implementation of Platform cache

Create Partition:

Follow below steps to create new cache partition in salesforce,

  1. Go to Setup=>Platform cache
  2. Click on ‘New Platform Cache Partition’
  3. Enter values as below in screenshots. As per the screenshot, 1MB space allocated to session cache and 2MB space allocated to Org cache.

Store and Retrieve values from Org cache:

  • Get partition using below code to store values in org cache
Cache.OrgPartition orgPart = Cache.Org.getPartition('local.CountryCode');
orgPart.put('INDIA', 'IN');
  • Use below code to retrieve the values from org cache.
String countrycode = (String)orgPart.get('INDIA');
  • Get partition using below code to store values in session cache.
// Get partition
Cache.SessionPartition sessionPart = Cache.Session.getPartition('local.CountryCode');
// Add cache value to the partition
sessionPart.put('INDIA', 'IN');
// Retrieve cache value from the partition
String countrycode = (String)sessionPart.get('INDIA');

Example to use org cache in salesforce:

Scenario: ABC Ltd has to deal with multiple countries and hence, stored country specific codes in custom object named ‘CountryCode’. Country codes never change but require to fetch from custom object multiple time in Apex code. hence, it is good use case to store the codes in platform cache.

  • Create below apex controller to store country codes in cache.
public class CountryCodes {

@AuraEnabled(cacheable=true)
public static list<CountryCode__c> getCountryCode()
{
Cache.OrgPartition orgPart = Cache.Org.getPartition('local.CountryCode');

list<CountryCode__c> lstCountryCode = (list<CountryCode__c>)orgPart.get('CountryCode');
if(lstCountryCode==null )
{
lstCountryCode=new list<CountryCode__c>();
lstCountryCode=[select id,Country_Name__c,Code__c from CountryCode__c];

}
return lstCountryCode;
}
}
  • Create LWC component to display country code.

CountryCode.html

<template>
<lightning-card title="Country code" icon-name="custom:custom14">
<ul class="slds-m-around_medium">
<template for:each={countrycode} for:item="country">
<li key={country.Country_Name__c}>{country.Country_Name__c}, {country.Code__c}</li>
</template>
</ul>
</lightning-card>
</template>

CountryCode.js

import { LightningElement ,wire,track} from 'lwc';
import getCountrycode from "@salesforce/apex/CountryCodes.getCountryCode";

export default class CountryCode extends LightningElement {
@track countrycode=[];

@wire(getCountrycode)
countrycodeHandler({data, error}){
if(data){
this.countrycode= JSON.parse(JSON.stringify(data));
}
if(error){
console.error(error)
}
}
}

countryCode.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__Tab</target>

</targets>
</LightningComponentBundle>
  • Create web tab for LWC.

Go to Setup=>Search tab=>Go to Lightning component tabs=>

Click on new

Select lightning component:‘CountryCode’

Give Tab label:’County Code Cache Example’

Select style and save.

  • Go to app launcher and search ’County Code Cache Example’

Use of Cache.CacheBuilder Interface to avoid cache misses will be covered in next article.

Reference:

--

--

No responses yet