Salesforce Platform Cache

Salesforce Platform Cache is a feature that allows you to store and access data in memory, providing faster access to frequently used or computationally expensive data. It's particularly useful for improving the performance of Salesforce applications by reducing response times and decreasing the load on backend systems.

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, and OrgPartition 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.

Session cache lives alongside a user session. The maximum life of a session is eight hours. 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 session cache, org cache is accessible across sessions, requests, and org users and profiles. Org cache expires when its specified time-to-live (ttlsecs value) is reached.

Additionally, Salesforce provides 3 MB of free Platform Cache capacity for security-reviewed managed packages through a capacity type called Provider Free capacity. You can allocate capacities to session cache and org cache from the Provider Free capacity.

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.

Example -

// Storing a complete list of records

Cache.OrgPartition accsPart=Cache.Org.getPartition('Accounts');
List< Account > accs=[select id, Name from Account];
accsPart.put('all', accs);

Cache.OrgPartition accsPart =Cache.Org.getPartition('Accounts');
List< Account > accfrmCache=(List< Account >) accsPart.get('all');
for (Account acc : accfrmCache)
{
    System.debug('Account  ' + acc);
}

--------------------------------------------------------------------------------------------------------------------------------

//Storing a single record


Cache.OrgPartition accPart=Cache.Org.getPartition('Accounts');

Account acc=[select id, Name from Account where id='001XXXXXXXXXX'];

accPart.put('001XXXXXXXXXX', acc);

Cache.OrgPartition accPart =Cache.Org.getPartition('Accounts');

Account accfrmCache =(Account) accPart.get('001XXXXXXXXXX');

System.debug('Account = ' + accFromCache);

--------------------------------------------------------------------------------------------------------------------------------

//Storing a list of Accounts

Cache.OrgPartition accsPart=Cache.Org.getPartition('Accounts');
List< Account > accs=[select id, Name from Account];
for (Account acc : accs)
{
    accsPart.put(acc.id, acc);
}

Cache.OrgPartition accsPart =Cache.Org.getPartition('Accounts');
List< Account > accs =[select id, Name from Account];
for (Account acc : accs)
{
    accsPart.put(acc.id, acc);

Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach