Newer Version Available
Storable Actions
Successful actions, for which getState() in the JavaScript callback returns SUCCESS, are stored.
If a storable action is aborted after it’s been sent but not yet returned from the server, its return value is still added to storage but the action callback is not called.
The action response of a storable action is saved in an internal framework-provided storage named actions. This stored response is returned on subsequent calls to the same server-side action instead of the response from the server-side controller, as long as the stored response hasn't expired.
If the stored response has reached its expiration time, a new response is retrieved from the server-side controller and is stored in the actions storage for subsequent calls.
Marking Storable Actions
To mark a server-side action as storable, call setStorable() on the action in JavaScript code, as follows.
1a.setStorable();The setStorable function takes an optional parameter, 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 refresh the stored item with a newly retrieved value, regardless of whether the item has expired or not. The default value is false.
To set the storage options for the action response, pass this configuration map into setStorable.
Refreshing an Action Response for Every Request
If a storable action returns dynamic content from the server, set the refresh interval to 0 to ensure that the data is refreshed from the server. If an action response is already cached, the cached response is displayed while the server roundtrip is happening.
To ignore existing stored responses, set:
1a.setStorable({
2 "ignoreExisting": "true"
3});Example
This example shows how to use setStorable() to store the server-side action response in a client-side cache. The markup includes a button that triggers the runActionAtServerAndStore client-side controller action. This client-side action calls a fetchDataRecord server-side action. Next, the action is marked as storable and is run. The server-side action return value is obtained in the callback.
This component markup initializes the actions storage and contains a button.
1<aura:component render="client" extensible="true"
2 controller="java://org.auraframework.impl.java.controller.AuraStorageTestController"
3 implements="auraStorage:refreshObserver">
4
5 <auraStorage:init debugLoggingEnabled="true"
6 name="actions"
7 secure="true"
8 persistent="false"
9 clearStorageOnInit="true"
10 defaultExpiration="50"
11 defaultAutoRefreshInterval="60" />
12
13 <ui:button label="Run action at Server and mark as storable"
14 press="{!c.runActionAtServerAndStore}"
15 aura:id="ForceActionAtServer"/>
16
17</aura:component>Here is the action in the component’s JavaScript client-side controller.
1runActionAtServerAndStore:function(cmp, evt, helper){
2 // Get server-side action
3 var action = cmp.get("c.fetchDataRecord");
4
5 action.setCallback(cmp, function(response){
6 var returnValue = response.getReturnValue();
7 });
8
9 // Set server-side action as storable
10 action.setStorable();
11
12 // Run server-side action
13 $A.enqueueAction(action);
14},