Platform Cache is Salesforce's distributed in-memory cache, accessible from Apex. It lets you store data across transactions/users to avoid redundant queries or computation.
Two cache types:
- Org Cache — shared across all users in the org. Use for reference data: tax rate tables, region mappings, configuration that's the same for everyone.
- Session Cache — per-user. Lasts the user's session. Use for user-specific computed data: their permission profile lookup, computed dashboard tiles.
API:
`apex // Set (with TTL in seconds) Cache.Org.put('tax_rates_v1', taxRateMap, 3600);
// Get Map<String,Decimal> rates = (Map<String,Decimal>) Cache.Org.get('tax_rates_v1'); if (rates == null) { rates = computeTaxRates(); // miss path - recompute Cache.Org.put('tax_rates_v1', rates, 3600); } `
Capacity:
- Org Cache: tied to allocation purchased; default Enterprise gets a few MB, larger orgs purchase more.
- Session Cache: smaller per-user.
- Each value is up to 100 KB.
Eviction:
- Time-based via TTL.
- LRU eviction when capacity hits.
- Cache misses are normal — always handle null gracefully.
Use cases:
- Expensive query results that don't change often: country/state lists, custom metadata aggregates.
- External API responses with a TTL: weather, exchange rates, third-party lookups (avoid repeated API limits).
- Computed authorisation matrices per user (Session Cache).
Don't use for:
- Mutable user data — you can't guarantee cache freshness across users.
- Anything sensitive — cache is in shared memory; encrypt at rest if storing PII.
- Large objects — 100 KB cap per value.
A common pattern: sweet spot for Custom Metadata Type lookups, where Custom Metadata is already cached by Apex but Platform Cache lets you cache derived values from Custom Metadata records.
