Caching Data Within Aura & Lightning Web Components

Salesforce Logo

Sharing is Caring

The Lightning Data Service really simplifies our development within Aura and within Lightning Web Components. The Aura versions of the lighting-record-form, lightning-record-edit-form and lightning-record-view-form and the Lightning versions really make it very simple to create, update, and view data without potentially having to write any Apex code.

A lot of people, don’t realize though that you can also implement caching in the same way without having to do much other than annotate your apex methods.

For the most part, I believe that using storable actions is a best practice for data that won’t change. It really helps to make your pages and components feel much snappier. (It also reduces platform usage which can be a huge win!).

In api versions greater 43 we can simply annotate our @AuraEnabled methods in Apex with the cacheable=true attribute and avoid calling setStorable.

For example, something like this:

@AuraEnabled (cachable=true)
    global static List<ContentDocumentLink> getRelatedFiles (Id someId) {
        return [SELECT ContentDocumentId,
            FROM ContentDocumentLink where LinkedEntityId =: someId];
Sharing is Caring