Sub topic 5
Read-only methods with cacheable=true
This chapter assumes you know basic Apex (classes, SOQL, DML), the basics of Lightning web components, and what @AuraEnabled does. Here you will learn what cacheable=true promises, what it forbids, and how it changes the way a component behaves.
What cacheable=true means
Adding cacheable=true to @AuraEnabled tells the platform that the method only reads data. Lightning Experience can then store the result on the client and reuse it. A repeat call with the same parameters can be answered from that cache, without a round trip to the server.
public with sharing class ContactController {
@AuraEnabled(cacheable=true)
public static List<Contact> getContacts(Id accountId) {
return [
SELECT Id, Name, Email
FROM Contact
WHERE AccountId = :accountId
WITH USER_MODE
ORDER BY Name
LIMIT 50
];
}
}The cache key is the method plus its parameter values. Calling getContacts for two different accounts creates two cache entries.
The read-only rule
A cacheable method must not change data. The platform enforces this. If the method performs DML, such as insert, update, or delete, the call fails at runtime. The same restriction applies to anything that would change the database state in the same transaction.
| Need | Use |
|---|---|
| Read data, safe to reuse | `@AuraEnabled(cacheable=true)` |
| Create, update, or delete data | `@AuraEnabled` (non-cacheable) |
If a method needs to write, leave out cacheable=true. Don't try to work around the restriction.
Why it matters to the component
Two features of Lightning web components require a cacheable method:
- The
@wiredecorator can only call Apex methods markedcacheable=true. - A cacheable method can also be called imperatively, and the result may still come from the cache.
A non-cacheable method can only be called imperatively. The next chapter covers @wire in detail. Here is a short preview of how the method above is used:
import { LightningElement, api, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactList extends LightningElement {
@api recordId;
@wire(getContacts, { accountId: '$recordId' })
contacts;
}The '$recordId' string makes the parameter reactive. When recordId changes, the wire runs again, and the platform either returns a cached result or calls the server.
How the cache behaves
The practical effect is that data can be stale. If another user, or your own code, changes a contact, the cached list does not update by itself. Refreshing it is covered in a later chapter on refreshApex.
Common errors
- Adding DML to a cacheable method. The call fails. Move the write into a separate non-cacheable method.
- Using
@wirewith a non-cacheable method. The component cannot wire it. Either mark the methodcacheable=true, if it only reads, or call it imperatively. - Expecting fresh data every time. A cached result may be returned. If a screen must always show the latest state after a save, plan to refresh it.
- Forgetting to enforce permissions. Caching does not replace security. Keep
with sharingandWITH USER_MODEon the query, as in the example.
Choosing between the two
Ask one question: does this method change data? If not, and the component can tolerate a briefly cached result, use cacheable=true. If it writes, or the result must always be fresh from the server, leave it out and call it imperatively.
Exercise
A method returns a list of open cases for an account and also sets a field on each case to record that it was viewed. A developer marks it @AuraEnabled(cacheable=true) so it can be wired. What happens, and how should the design change?
Show answer
The call fails because a cacheable method cannot perform DML. Split it into two methods: a cacheable read method that returns the cases, and a separate non-cacheable @AuraEnabled method that records the view and is called imperatively.
Exercise
Two components call getContacts with the same accountId. After the first call, a user adds a contact elsewhere. Why might the second component still show the old list?
Show answer
The result is cached on the client by method and parameter values. The second call is answered from the cache, so it does not see the new contact until the cache is refreshed.