Platform Cache Considerations
Review these considerations when working with Platform Cache.
-
Cache isn’t persisted. There’s no guarantee against data loss.
-
Some or all cache is invalidated when you modify an Apex class in your org.
-
Data in the cache isn’t encrypted.
-
Org cache supports concurrent reads and writes across multiple simultaneous Apex transactions. For example, a transaction updates the key
PetNamewith the valueFido. At the same time, another transaction updates the same key with the valueFelix. Both writes succeed, but one of the two values is chosen arbitrarily as the winner, and later transactions read that one value. However, this arbitrary choice is per key rather than per transaction. For example, suppose one transaction writesPetType="Cat"andPetName="Felix". Then, at the same moment, another transaction writesPetType="Dog"andPetName="Fido". In this case, thePetTypewinning value could be from the first transaction, and thePetNamewinning value could be from the second transaction. Subsequentget()calls on those keys would returnPetType="Cat"andPetName="Fido". -
Cache misses can happen. We recommend constructing your code to consider a case where previously cached items aren’t found. Alternatively, use the CacheBuilder Interface, which checks for cache misses.
-
All platform cache statistical methods:
getAvgGetSize(),getAvgGetTime(),getMaxGetSize(),getMaxGetTime(), andgetMissRate()report data starting from the time the cache server was restarted, and do not include data prior to the restart. -
Partitions must adhere to the limits within Salesforce.
-
The session cache can store values up to eight hours. The org cache can store values up to 48 hours.
-
For orgs that use Salesforce Flow:
- When a process contains a scheduled action, make sure that later actions in the process don’t invoke Apex code that stores or retrieves values from the session cache. The session-cache restriction applies to Apex actions and to changes that the process makes to the database that cause Apex triggers to fire.
- When a flow contains a Pause element, make sure that later elements in the flow don’t invoke Apex code that stores or retrieves values from the session cache. The session-cache restriction applies to Apex actions and to changes that the flow makes to the database that cause Apex triggers to fire.