Storable Action in Lightning in Salesforce

Enhance your component’s performance by marking actions as storable to quickly show cached data from client-side storage without waiting for a server trip. If the cached data is stale, the framework retrieves the latest data from the server.

The storable action might result in no call to the server. Never mark as storable an action that updates or deletes data. For storable actions in the cache, the framework returns the cached response immediately and also refreshes the data if it’s stale. Therefore, storable actions might have their callbacks invoked more than once: first with cached data, then with updated data from the server.

To mark a server-side action as storable, call setStorable() on the action in JavaScript code, as follows.

action.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

To use ignoreExisting attribute:

        action.setStorable({
        ignoreExisting: true
        });

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.

Storable actions are automatically configured in Lightning Experience and the Salesforce mobile app. To use storable actions in a standalone app (.app resource), you must configure client-side storage for cached action responses. To configure client-side storage for your standalone app, use <auraStorage:init> in the auraPreInitBlock attribute of your application’s template. For example:

Sample Code:

<aura:component isTemplate="true" extends="aura:template">
    <aura:set attribute="auraPreInitBlock">
        <auraStorage:init
          name="actions"
          persistent="false"
          secure="true"
          maxSize="1024"
          defaultExpiration="900"
          defaultAutoRefreshInterval="30" />
    </aura:set>
</aura:component>

Properties:

name
The storage name must be actioned. Storable actions are the only currently supported type of storage.

persistent
Set to true to preserve cached data between user sessions in the browser.

secure
Set to true to encrypt cached data.

maxsize
The maximum size in KB of the storage.

defaultExpiration
The duration in seconds that an entry is retained in storage.

defaultAutoRefreshInterval
The duration in seconds before an entry is refreshed in storage.

How to abort the storage action?

Mark an action as abortable to make it potentially abortable while it's queued to be sent to the server. An abortable action in the queue is not sent to the server if the component that created the action is no longer valid, that is cmp.isValid() == false. A component is automatically destroyed and marked invalid by the framework when it is unrendered.

var action = cmp.get("c.serverEcho");
action.setAbortable();

No comments:

Post a Comment