Intermediate 15 min read

@AuraEnabled and cacheable=true

By the end of this lesson, you'll be able to:

  • Expose an Apex method to Lightning components with @AuraEnabled
  • Understand what cacheable=true does and when it is appropriate
  • Recognize why DML methods must never be marked cacheable=true

Prerequisites: "When Apex Is Required"

@AuraEnabled: The Bridge

public with sharing class AccountController {
    @AuraEnabled(cacheable=true)
    public static List<Account> getAccounts() {
        return [SELECT Id, Name, Industry FROM Account WITH SECURITY_ENFORCED LIMIT 50];
    }
}

@AuraEnabled is the annotation that exposes an Apex method to Lightning components. Legacy note: the name comes from the older Aura component framework (Module 1 covered Aura vs. LWC) — but the same annotation is what LWC uses today too. There is no separate "LWC-only" version of this annotation.

cacheable=true

cacheable=true marks a method as read-only, enabling the same kind of client-side caching Lightning Data Service provides automatically for record reads (Module 6). It is required for any Apex method used with @wire, and must never be set on a method that performs DML (insert/update/delete) — a cacheable method is assumed safe to call repeatedly and reuse cached results, which is only true if it never changes data.

WITH SECURITY_ENFORCED

Unlike LDS, which enforces field-level security and sharing automatically, a hand-written SOQL query does not do this by default. WITH SECURITY_ENFORCED on the query (as shown above) makes Apex throw if the running user lacks access to any queried field — an explicit, necessary safeguard that has no equivalent needed in LDS-based components.

Why This Matters in Real Projects

Getting cacheable=true wrong in either direction is a real, common bug: forgetting it on a read-only method used with @wire causes a deployment or runtime error; setting it on a method that performs DML causes a silent correctness bug where stale cached data masks a real write.

Exercise

Write an @AuraEnabled Apex method signature for a read-only method named getContactsByAccountId that takes an Account Id and returns a List<Contact>.

Show hint

Remember cacheable=true for a read-only method.

APEX

Exercise

Challenge: explain, as a comment, why a method that performs an insert DML operation must NOT be marked cacheable=true.

Show hint

Think about what caching assumes about a method's behavior.

APEX

@AuraEnabled and cacheable=true Quiz

1. What does @AuraEnabled do?

2. Where does the name "@AuraEnabled" originate from?

3. cacheable=true can be safely used on a method that performs an insert or update.

4. What does WITH SECURITY_ENFORCED do in a SOQL query?

5. When is cacheable=true required?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

@AuraEnabled is the bridge that exposes an Apex method to Lightning components — a legacy name from the Aura framework era, still used for LWC today — and cacheable=true marks read-only methods for the same client-side caching benefits LDS provides automatically.