Thursday 15 November 2018

Platform Cache in Salesforce | Platform Cache Basics


What is Platform Cache ?

"Platform Cache" is a memory layer that store's Salesforce session and org data for later access. "Platform Cache" is just like a RAM for your app. With Platform Cache, your applications can run faster because they store reusable data in memory. "Platform Cache" is used to to store Static Data, complex computations  and Frequently used data.

Point for consideration and Best Practices :-
  • Performance , Enterprise and Unlimited Org have default cache space included.
  • ISV's can buy cache for there application .
  • Sold in 10 MB blocks. 
  • Data is not persisted, There is no guaranty of data loss
  • Session Data can be stored up to 8 hours only.
  • Org cache can store up to 48 hours.
  • $Cache.Session Global Variable can be used in Visualforce Pages.
  • Store Data That Doesn’t Change Often
  • Data in the cache isn’t encrypted
  • Partition Size Limits :- 5 MB per Partition.
  • Maximum size of a single cached item 100KB

Type of Platform Cache :-
1) Org Cache :- Org wide data for anyone in the org
2) Session Cache :- Data for a specific user stored up to 8 hours.

How to Setup Platform Cache :-
Step 1) Click on Setup then Type "Platform Cache" in Quick Find. Then click on "Platform Cache"
Step 2) Request for Free Trial if "Platform Cache" is not available for your org (Optional Step)

Step 3) Then click on "New Platform Cache Partition" and the provide the "Name" all required detail
Step 4) Make the first Partition as Default Partition.



Store and Retrieve Data in Org Cache :-

After partitions setup, we can use the Cache.SessionPartition and Cache.OrgPartition class's to add, retrieve, or remove values from a partition’s cache. Use Cache.Session and Cache.Org classes to get a partition or perform cache operations by using a fully qualified key.



Handling Session cache:-

// Add a value to the cache . local is the default name space cache
Cache.Session.put('local.MyFirstPartition.key', '1234567');

// Check value is there in Cache
if (Cache.Session.contains('local.MyFirstPartition.key')) {
    // get Cache value
    String key = (String)Cache.Session.get('local.MyFirstPartition.key');
}

Handling Org Cache:-


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

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

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

Reference
1) https://trailhead.salesforce.com/content/learn/modules/platform_cache/platform_cache_get_started
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_platform_cache_session_examples.htm


Please join our YouTube Channel. Please check below related post

1Component Event in Lightning Component | aura:event  
2) Invoke Apex from Lightning Component
3) Design Resource In Lightning Component Bundle
4) Create and Destroy Modal Dialog Component

5) Inheritance In Lightning Component
6) Dynamic Picklists in Custom Component Property





Thanks,
Amit Chaudhary

Capture.JPG  @amit_sfdc    @ApexHours
  Salesforce Apex Hours 
    #SalesforceApexHours 

2 comments:

  1. Hello amit,
    i have to make community custom theme layout in which login component is there.......
    after loin successfully i have to check user is still login or not for the rest of the page.....
    can u guide me.....
    i don't know from where to start where to store that cache value......

    ReplyDelete
  2. Hi Amit,
    You not be calling the contains(key) method followed by the get(key) method. If you intend to use the key value, simply call the get(key) method and make sure that the value is not equal to null.
    Cache can be invalidate anytime, in particular between the two instructions.
    You can refer to Best Practices here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_platform_cache_best_practices.htm

    ReplyDelete