Platform Cache in Salesforce

Platform Cache is a memory layer that stores Salesforce session and org data for later access. When you use Platform Cache, your applications can run faster because they store reusable data in memory. Applications can quickly access this data; they don’t need to duplicate calculations and requests to the database on subsequent transactions. In short, think of Platform Cache as RAM for your cloud application.

With Platform Cache, you can also allocate cache space so that some apps or operations don’t steal capacity from others. You use partitions to distribute space. We’ll get to partitions later.

1. Go to Platform Cache.

2. Request Trial Capacity.

3. Create a New Platform Cache Partition.

4. Save a New Platform Cache Partition.

Sample Apex Class:


public class BusScheduleCache {
    private Cache.OrgPartition part;
    
    public BusScheduleCache() {
        Cache.OrgPartition orgPart = Cache.Org.getPartition(‘local.BusSchedule’);
    }
    
    public void putSchedule(String busLine, Time[] schedule) {
        // Get partition
        Cache.OrgPartition orgPart = Cache.Org.getPartition(‘local.BusSchedule’);
        
        // Add cache value to the partition. Usually, the value is obtained from a callout
        orgPart.put(busLine, schedule);
    }
    
    public Time[] getSchedule(String busLine) {
        Cache.OrgPartition orgPart = Cache.Org.getPartition(‘local.BusSchedule’);
        Time[] listTimes = new Time[]{};
        Time[] listTimes1 = (Time[])orgPart.get(‘BusSchedule’);
        if (listTimes1 == null) {
            listTimes.add(Time.newInstance(8, 0, 0, 0));
            listTimes.add(Time.newInstance(17, 0, 0, 0));
        } else {
            listTimes = listTimes1;
        }
        return listTimes;
    }
}
Cheers!!!

Leave a Reply