Sub topic 9
What cacheable=true means and when you can use it
This chapter assumes you can write an @AuraEnabled method, import it into a Lightning Web Component, and call it imperatively. It covers what cacheable=true changes and how to decide whether to set it.
What the option does
@AuraEnabled(cacheable=true) tells the platform that a method only reads data. The platform may then store the response on the client and reuse it for later calls with the same parameters. This saves a round trip to the server.
The option makes a promise on your behalf, and the platform enforces it.
- The method must not perform DML (insert, update, delete, upsert). A DML statement in a cacheable method fails at runtime.
- The method should have no other side effects. A second call with the same arguments should give the same answer, because the platform may skip running it.
- The method must be
static, like every@AuraEnabledmethod used by LWC.
Where it is required
The wire service needs cacheable=true. Wiring a method that lacks it fails. Imperative calls work either way, so for them it is a design choice.
| Situation | cacheable=true | Why |
|---|---|---|
| Method used with `@wire` | Required | The wire service reads cached data and re-provisions it. |
| Read-only method called imperatively | Optional | Gives caching, but results may be stale. |
| Method that performs DML | Not allowed | Cached responses would skip the write. |
| Read that must always be current | Avoid | Each call should reach the server. |
Example: one read method, one write method
The controller below splits the work. The search is cacheable. The update is not.
public with sharing class ContactController {
@AuraEnabled(cacheable=true)
public static List<Contact> searchContacts(String searchTerm) {
String key = '%' + searchTerm + '%';
return [
SELECT Id, Name, Email
FROM Contact
WHERE Name LIKE :key
WITH USER_MODE
ORDER BY Name
LIMIT 20
];
}
@AuraEnabled
public static void updateEmail(Id contactId, String email) {
try {
Contact c = new Contact(Id = contactId, Email = email);
update as user c;
} catch (DmlException e) {
throw new AuraHandledException(e.getDmlMessage(0));
}
}
}Both methods run with sharing and in user mode, so cacheable and non-cacheable methods follow the same security rules.
The stale data trade-off
Caching helps only if the data does not change often. If another user, a trigger, or your own updateEmail call changes a contact, a cached searchContacts result can still show the old email. The platform does not know that your write affects that read.
Here is the decision as a diagram.
Practical guidance
- Default to
cacheable=truefor pure queries that feed lists, lookups, and displays. - Leave it off for data that must be fresh on every call, such as a balance or a status checked right after a save.
- Keep reads and writes in separate methods, as the controller above does.
- When a cacheable method is wired, refresh it after a change with
refreshApex. Chapter material on refreshing covers this. - Treat data returned through the wire service as read-only. Copy it before you modify it in JavaScript.
If you see a "DML operation not allowed" style error when a component loads, check whether the method you imported is marked cacheable.
Exercise
A method getOpenCases() runs a SOQL query and must show current data immediately after a user closes a case through closeCase(). Which of the two methods can be cacheable, and what risk does making getOpenCases() cacheable create?
Show answer
getOpenCases() can be cacheable because it only reads. closeCase() performs DML, so it cannot be. If getOpenCases() is cacheable, a repeated call with the same parameters may return the cached list, which still contains the case that was just closed. To avoid this, leave getOpenCases() non-cacheable, or use the wire service and call refreshApex after closeCase() finishes.