Skip to main content

Salesforce Platform Cache

 Salesforce Platform Cache is a feature that allows you to store and manage data in-memory, improving the performance of your Salesforce applications. It provides two types of caches: Org Cache and Session Cache. Org Cache is shared across all users in your Salesforce organization, while Session Cache is specific to a single user's session. Here, I'll provide an example of how to use Salesforce Platform Cache with Apex.

Example Scenario: Let's consider a scenario where you have a Salesforce custom object called "Product__c," and you want to cache the product data for better performance.

Step 1: Enable Platform Cache Before you can use Platform Cache, you need to enable it in your Salesforce org. To do this, go to Setup > Quick Find Box > Type "Platform Cache" > Select "Platform Cache."

Step 2: Create a Cache Partition Create a cache partition, which is like a container for storing data in Platform Cache. Each partition has a unique name and can store data with a specific scope (e.g., org-wide or session-specific).

  1. Go to Setup > Quick Find Box > Type "Platform Cache" > Select "Platform Cache Partitions."
  2. Create a new partition with a unique name, e.g., "ProductCache."

Step 3: Apex Code for Caching Product Data

Now, you can use Apex code to store and retrieve product data in the Platform Cache.

public class ProductCacheService { // Define the name of the cache partition private static final String CACHE_PARTITION_NAME = 'ProductCache'; // Define a method to get product data from the cache public static List<Product__c> getProducts() { Cache.OrgPartition orgPartition = Cache.Org.getPartition(CACHE_PARTITION_NAME); List<Product__c> products = (List<Product__c>)orgPartition.get('AllProducts'); if (products == null) { // Cache miss, fetch data from the database and cache it products = [SELECT Id, Name, Price__c FROM Product__c]; orgPartition.put('AllProducts', products); } return products; } // Define a method to clear the cached product data public static void clearProductCache() { Cache.OrgPartition orgPartition = Cache.Org.getPartition(CACHE_PARTITION_NAME); orgPartition.remove('AllProducts'); } }

Step 4: Using the Cache in Your Apex Code

Now, you can use the ProductCacheService in your Apex controllers or classes to retrieve and cache product data:

List<Product__c> products = ProductCacheService.getProducts();

By using Salesforce Platform Cache, you can significantly improve the performance of your Salesforce applications by reducing the need to repeatedly query the database for the same data.

However, it comes with certain limitations and considerations that you should be aware of:


Comments

Popular posts from this blog

API

API is a application programming interface that allow different application interact with each other.Its deliver the request to your provider that you are requesting it from and deliver the response back. A real example of an API How are APIs used in the real world? Here’s a very common scenario –  booking a flight. When you search for flights online, you have a menu of options to choose from. You choose a departure city and date, a return city and date, cabin class, and other variables like your meal, your seat, or baggage requests. To book your flight, you need to interact with the airline’s website to access the airline’s database to see if any seats are available on those dates, and what the cost might be based on the date, flight time, route popularity, etc. You need access to that information from the airline’s database, whether you’re interacting with it from the website or an online travel service that aggregates information from multiple airlines. Alter...

Identity and access management Flow

Salesforce can participate in a number of standard OAuth 2.0 flows acting as either the client, or as the authorisation and resource server. The flows in this section are the common general flows which are used for either user or server authorisation and authentication - flows to be applied in specific circumstances can be found in the "Specialised OAuth 2.0 Flows" section. The diagrams are shown in general terms as Salesforce can play the role of client or server. Considerations specific to Salesforce are mentioned in the notes. WEB SERVER FLOW Based on the OAuth Authorisation Code Grant flow Gold standard for security if the client secret can be protected by the client application - generally this is the case when the application is operating in a trusted environment (e.g. an internal enterprise server application) Code challenge is optional and used together with code verifier in the token request to provide PKCE (p...