Platform Cache is a memory layer that stores your application's session and environment data for later access. Applications run faster because they store reusable data instead of retrieving it whenever needed. Note that Platform Cache is visible and mutable by default and should never be used as a database replacement. Developers should use cache only for static data that is either frequently needed or computationally expensive to acquire. Let's explore the use of cache in a simple Apex class.
@AuraEnabled(cacheable=true) | |
public static List<Options> fetchObjectNamesUsingGlobalDescribe() { | |
List<Options> objectNames = new List<Options>(); | |
try { | |
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); | |
for (String objectName : schemaMap.keySet()) { | |
Schema.DescribeObjectResult describeResult = schemaMap.get( | |
objectName | |
) | |
.getDescribe(SObjectDescribeOptions.DEFERRED); | |
if (describeResult.isQueryable()) { | |
objectNames.add( | |
new Options(describeResult.getLabel(), objectName) | |
); | |
} | |
} | |
} catch (exception e) { | |
throw new AuraHandledException(e.getMessage()); | |
} | |
return objectNames | |
} | |
// CPU Time - 1307 ms | |
// Heap Size - 80,000 bytes |
In the example above, we acquire objects in the environment to create a schema. The Schema.getGlobalDescribe()
function returns a map of all sObject names (keys) to sObject tokens (values) for the standard and custom objects defined in the environment in which we're executing the code. Unfortunately, we're not caching the data, which makes this an expensive process. This code consumes 1,307 ms of CPU time with a heap size of 80,000 bytes. Let's improve this code by using a cache partition.
public static List<Options> fetchObjectNamesUsingGlobalDescribeFromCache() { | |
List<Options> objectNames = new List<Options>(); | |
try { | |
// Instantiate cache partition | |
Cache.OrgPartition orgPartition = Cache.Org.getPartition('CACHE_PARTITION_NAME'); | |
if (orgPartition != null) { | |
// Load from Cache | |
if (orgPartition.get('objectlistfromdescribe') != null) { | |
objectNames = (List<Options>) orgPartition.get('objectlistfromdescribe'); | |
} else { | |
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); | |
// Same as previous code | |
List<Options> objectNamesViaDescribe = ; //values populated from schema describe | |
// Put the values into the org partition | |
orgPartition.put('objectlistfromdescribe', objectNamesViaDescribe, 300, | |
Cache.Visibility.ALL, | |
true | |
); | |
} | |
} | |
} | |
return objectNamesViaDescribe; | |
} | |
// CPU Time - 20 ms | |
// Heap Size - 1,300 bytes |
This code performs the same operation but caches the result. In line 5, we're instantiating a cache partition. We're running the same function to build our schema map; however, line 15 instructs the program to place the results in the cache for later use. Our processing requirements diminished significantly, consuming only 20 ms of CPU time.
Despite the breathtaking advances in processing power, developers should always ensure they are writing efficient code that possesses a minimal processing footprint and scales with increased volume.