Saturday, June 6, 2020

Platform Cache in SalesForce

The Lightning Platform Cache layer provides faster performance and better reliability when caching Salesforce session and org data. Specify what to cache and for how long without using custom objects and settings or overloading a Visualforce view state. Platform Cache improves performance by distributing cache space so that some applications or operations don’t steal capacity from others.
Because Apex runs in a multi-tenant environment with cached data living alongside internally cached data, caching involves minimal disruption to core Salesforce processes.

Platform Cache Features

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 SessionOrgSessionPartition, 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.
The best data to cache is:
  • Reused throughout a session
  • Static (not rapidly changing)
  • Otherwise expensive to retrieve

Handling Org Cache:-

// Get partition
Cache.OrgPartition orgPart = Cache.Org.getPartition('local.FirstPartition');

// Add cache value to the partition
orgPart.put('key','welcome All');

// Retrieve cache value from the partition
if (orgPart.contains('key')) {
    String key = (String)orgPart.get('key');
}

Handling Session cache:-
// Add a value to the cache . local is the default name space cache Cache.Session.put('local.MyFirstPartition.key', '123456789'); // Check value is there in Cache if (Cache.Session.contains('local.FirstPartition.key')) {     // get Cache value     String key = (String)Cache.Session.get('local.FirstPartition.key'); }
Best Practices

1)Cache lists or maps of objects rather than single objects.
2)Maximum size of a single cached item 100KB
3)Use a wrapper class to reduce sObject overhead
4)Use the fully qualified name of your cache partition
5)Session Data can be stored up to 8 hours only.
6)Org cache can store up to 48 hours.


For more details please visit below links 
 1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_cache_namespace_overview.htm
 2) https://trailhead.salesforce.com/content/learn/modules/platform_cache