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 Cache.Session, Cache.Org, Session.Partition, and Org.Partition classes 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.
There are two types of cache:
-
Session cache—Stores data for individual user sessions. For example, in an app
that finds customers within specified territories, the calculations that run while users
browse different locations on a map are reused.
A session cache lives alongside a user
session. The maximum life of a session is eight hours. A session cache expires when its
specified time-to-live (ttlsecs value) is reached or when the session
expires after eight hours, whichever comes first.
-
Org cache—Stores data that any user in an org reuses. For example, the contents
of navigation bars that dynamically display menu items based on user profile are
reused.
Unlike a session cache, an org cache is accessible across sessions, requests,
and org users and profiles. An org cache expires when its specified time-to-live
(ttlsecs value) is reached.
The best data to cache is:
- Reused throughout a session
- Static (not rapidly changing)
- Otherwise expensive to retrieve
For both session and org caches, you can construct calls so that cached data in one namespace
isn’t overwritten by similar data in another. Optionally use the
Cache.Visibility enumeration to specify whether Apex code can access cached
data in a namespace outside of the invoking namespace.
Each cache operation depends on the Apex transaction within
which it runs. If the entire transaction fails, all cache operations in that transaction are
rolled back.