Caching Data Within Aura & Lightning Web Components

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,
                ContentDocument.Title,
                LinkedEntityId
            FROM ContentDocumentLink where LinkedEntityId =: someId];
    }
Sharing is Caring

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.