Content Header
Salesforce Platform Cache
Caching is an important aspect of the Salesforce platform, as it allows for faster access to data and instructions by storing them temporarily in memory. The Salesforce platform has several different types of caches that provide different benefits depending on the feature being used.
Cache Memory
Cache memory is a type of memory that stores data and instructions temporarily to allow for faster access. It is faster than main memory because it is physically closer to the processor, but there are still limitations to its size. The size of a company's cache depends on the amount of work being done in Salesforce and the number of objects in the system, with smaller companies generally having smaller caches due to their lower resource usage.
Benefits of Caching
Caching can provide several benefits in a Salesforce platform environment. It can improve performance by reducing the number of database queries and allowing for faster access to data. It can also reduce the load on the server by storing frequently accessed data in memory rather than constantly retrieving it from the database.
Additionally, caching can improve the scalability of the system by allowing it to handle more users and requests without sacrificing performance.
Different Levels of Caching
The Salesforce platform has three main levels of caching: application cache, session cache, and org cache.
The application cache stores static content that does not change frequently, such as customer information. This allows users to access the data more quickly without having to wait for it to be reloaded from the database.
The session cache stores user sessions across multiple requests, only sending out new elements when needed rather than all elements every time a page is accessed. This reduces bandwidth usage significantly.
The org cache allows users who have access rights for an organization's lead profile or contact records, such as sales reps, customers' managers, and partners, to access these records without having to set up individual access credentials each time they visit these pages within a Salesforce instance.
-
Application Cache
The application cache is used to store static content that does not change frequently, such as customer information or product descriptions. This allows users to access the data more quickly without having to wait for it to be reloaded from the database.
Using the application cache can improve performance by reducing the number of database queries and allowing for faster access to data. It can also reduce the load on the server by storing frequently accessed data in memory rather than constantly retrieving it from the database.
To use the application cache in Apex, you can use the **`Cache`** class and its various methods.
Here is an example of how to store a value in the application cache:
Cache.put('key', value);
And here is an example of how to retrieve a value from the application cache:
Object value = Cache.get('key');
You can also specify an expiration time for the cache entry using the **`Cache.put()`** method:
Cache.put('key', value, 60); // cache entry expires in 60 seconds
Session Cache
The session cache is used to store objects for the duration of a user's session. This allows multiple users to access the same objects, such as reports or templates, without having to retrieve them from the database each time.
Session cache is not available in Lightning Experience, but it is available in Salesforce Classic and Sales Cloud Professional Edition. It is also supported by all versions of Salesforce e-commerce solutions, including ApexPages, VisualForce pages, Lightning Components, and Visual Builder components.
Using the session cache can improve performance by reducing the number of database queries and allowing for faster access to data. It can also reduce the load on the server by storing frequently accessed data in memory rather than constantly retrieving it from the database.
To use the session cache in Apex, you can use the **`ApexPages`** class and its **`getSessionState()`** and **`putSessionState()`** methods.
Here is an example of how to store a value in the session cache:
ApexPages.putSessionState('key', value);
And here is an example of how to retrieve a value from the session cache:
Object value = ApexPages.getSessionState('key');
Org Cache
The org cache is a global, shared cache that is available to all organizations in an account. It stores data that can be accessed across all Salesforce orgs, such as CRM information and lead details.
The org cache stores these objects in memory and updates them automatically when new data arrives in the system. It is also used to store frequently updated objects like templates, reports, and forms, so they do not need constant refreshing from the database.
Using the org cache can improve performance by reducing the number of database queries and allowing for faster access to data. It can also improve scalability by allowing the system to handle more users and requests without sacrificing performance.
To use the org cache in Apex, you can use the **`OrgCache`** class and its **`get()`** and **`put()`** methods.
Here is an example of how to store a value in the org cache:
OrgCache.put('key', value);
And here is an example of how to retrieve a value from the org cache:
Object value = OrgCache.get('key');
Note that the org cache is available in API version 46.0 and later.
Conclusion
In summary, the Salesforce platform has three main levels of caching: application cache, session cache, and org cache. These caches store data in memory to allow for faster access and improve performance and scalability. Understanding how these caches work and how to use them effectively can help you get the most out of your Salesforce environment.
Caching is an important aspect of the Salesforce platform, providing faster access to data and improving performance and scalability. Understanding the different levels of caching and how they work can help you get the most out of your Salesforce environment.

