function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
GovindarajGovindaraj 

Platform Cache vs @Auraenabled(cacheable=true)

Hi,
Can anyone explain me the differene between Platform cache and @Auraenabled(cacheable=true)

I could see both are used for caching. Is there any specific use case to use them ?

Thanks,
Govindaraj.S
Jean-Luc KABORE-TURQUIN 1Jean-Luc KABORE-TURQUIN 1
Hey Govindaraj! Using the Dev. Guide, I could resume it that way:

#The Platform Cache* API lets you store and retrieve data that’s tied to Salesforce sessions or shared across your org>
         -There is no guarantee against Data loss.
         +Org cache supports concurrent reads and writes across multiple simultaneous Apex transactions.
        
To learn more about the Platform Cache Features :https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_platform_cache_features.htm

#@AuraEnabled annotation** enables client- and server-side access to an Apex controller method
        -In API version 44.0 and later, you can improve runtime performance by caching method results on the client by using the annotation @AuraEnabled(cacheable=true).
        -Using this annotation eliminates the need to call setStorable() in JavaScript code on every action that calls the Apex method.


*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.

**An Apex annotation modifies the way that a method or class is used, similar to annotations in Java. Annotations are defined with an initial @ symbol, followed by the appropriate keyword.


I hope I answered your question and wish you a good day.

Regards,

Jean-Luc
AbhishekAbhishek (Salesforce Developers) 
@AuraEnabled( cacheable = true )

To improve runtime performance, set @AuraEnabled(cacheable=true) to cache the method results on the client. To set cacheable=true, a method must only get data. It can’t mutate data.

Marking a method as storable improves your component’s performance by quickly showing cached data from client-side storage without waiting for a server trip. If the cached data is stale, the framework retrieves the latest data from the server. Caching is especially beneficial for users on high-latency, slow, or unreliable connections, such as 3G networks.

Prior to Winter ’19, to cache data returned from an Apex method, you had to call setStorable() in JavaScript code on every action that called the Apex method. Now you can mark the Apex method as storable (cacheable) and get rid of any setStorable() calls in JavaScript code.

Example - http://www.infallibletechie.com/2019/03/how-to-invoke-apex-controller-from.html



"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 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

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.