Platform Cache
The Lightning Platform Cache layer provides faster performance and better reliability when caching Salesforce session and org data. Specify what to cache and for how long without using custom objects and settings or overloading a Visualforce view state. Platform Cache improves performance by distributing cache space so that some applications or operations don’t steal capacity from others.
Because Apex runs in a multi-tenant environment with cached data living alongside internally cached data, caching involves minimal disruption to core Salesforce processes.
-
The Platform Cache API lets you store and retrieve data that’s tied to Salesforce sessions or shared across your org. Put, retrieve, or remove cache values by using the
Session,Org,SessionPartition, andOrgPartitionclasses in the Cache namespace. Use the Platform Cache Partition tool in Setup to create or remove org partitions and allocate their cache capacities to balance performance across apps. -
Review these considerations when working with Platform Cache.
-
These limits apply when using Platform Cache.
-
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.
-
Platform Cache uses local cache and a least recently used (LRU) algorithm to improve performance.
-
Store and Retrieve Values from the Session Cache
Use the
Cache.SessionandCache.SessionPartitionclasses to manage values in the session cache. To manage values in any partition, use the methods in theCache.Sessionclass. If you’re managing cache values in one partition, use theCache.SessionPartitionmethods instead. -
Store and Retrieve Values from the Org Cache
Use the
Cache.OrgandCache.OrgPartitionclasses to manage values in the org cache. To manage values in any partition, use the methods in theCache.Orgclass. If you’re managing cache values in one partition, use theCache.OrgPartitionmethods instead. -
Use a Visualforce Global Variable for the Platform Cache
You can access cached values stored in the session or org cache from a Visualforce page with global variables.
-
Safely Cache Values with the CacheBuilder Interface
A Platform Cache best practice is to ensure that your Apex code handles cache misses by testing for cache requests that return null. You can write this code yourself. Or, you can use the
Cache.CacheBuilderinterface, which makes it easy to safely store and retrieve values to a session or org cache. -
Platform Cache can greatly improve performance in your applications. However, it’s important to follow these guidelines to get the best cache performance. In general, it’s more efficient to cache a few large items than to cache many small items separately. Also be mindful of cache limits to prevent unexpected cache evictions.