Newer Version Available
Storable Actions
Most server requests are read-only and idempotent, which means that a request can be repeated or retried as often as necessary without causing data changes. The responses to idempotent actions can be cached and quickly reused for subsequent identical actions. For storable actions, the key for determining an identical action is a combination of:
- Apex controller name
- Method name
- Method parameter values
Marking an Action as Storable
To cache data returned from an Apex method for any component with an API version of 44.0 or higher, you must annotate the Apex method with @AuraEnabled(cacheable=true). For example:
1@AuraEnabled(cacheable=true)
2public static Account getAccount(Id accountId) {
3 // your code here
4}Prior to API version 44.0, to cache data returned from an Apex method, you had to call setStorable() in JavaScript code on every action that called the Apex method. For API version of 44.0 or higher, you can mark the Apex method as storable (cacheable) and get rid of any setStorable() calls in JavaScript code. The Apex annotation approach is better because it centralizes your caching notation for a method in the Apex class.
Call setStorable() on an action in JavaScript code, as follows.
1action.setStorable();The setStorable function takes an optional argument, which is a configuration map of key-value pairs representing the storage options and values to set. You can only set the following property:
- ignoreExisting
- Set to true to bypass the cache. The default value is false.
- This property is useful when you know that any cached data is invalid, such as after a record modification. This property should be used rarely because it explicitly defeats caching.
To set the storage options for the action response, pass this configuration map into setStorable(configObj).