Activity Forums Salesforce® Discussions what is the use of setStorable() in Salesforce?

  • Anjali

    Member
    September 5, 2018 at 1:46 pm

    Hi Prachi,

    A storable action is a server action whose response is stored in the client cache so that subsequent requests for the same server method with the same set of arguments can be accessed from that cache.Server action is an Apex method that you invoke remotely from your Lightning Component.

    To make an action storable, you simply call its setStorable() function.

    For example:

    var action = component.get("c.getListOfItem");
        action.setStorable();
        action.setCallback(this, function(response) {
        // handle response
    };
    $A.enqueueAction(action);

    When an action is marked as storable, the framework automatically returns the response from the client cache (if available) so that the data is immediately available to the component for display or processing. The framework might then call the server method in the background, and if the response is different, invoke the action callback function a second time.

    Hope it may help you.

  • shariq

    Member
    September 14, 2018 at 10:56 pm

    Hi,

    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).
    Lifecycle of Storable Actions
    This image describes the sequence of callback execution for storable actions.
    Enable Storable Actions in an Application
    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.
    Storage Service Adapters
    The Storage Service supports multiple implementations of storage and selects an adapter at runtime based on browser support and specified characteristics of persistence and security. Storage can be persistent and secure. With persistent storage, cached data is preserved between user sessions in the browser. With secure storage, cached data is encrypted.
    Hope this helps.

  • prabhat

    Member
    February 5, 2019 at 7:20 am

    Hi Prachi,

    Prior to Winter ’19, to cache data returned from an Apex method, you had to call setStorable() in JavaScript code on every action that called the Apex method. Now you can mark the Apex method as storable (cacheable) and get rid of any setStorable() calls in JavaScript code.

    Marking a method as storable improves your component’s performance by quickly showing 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. Caching is especially beneficial for users on high-latency, slow, or unreliable connections, such as 3G networks.

    This change is versioned. Existing component code with an API version of 43.0 or earlier continues to work without any changes.

    To cache data returned from an Apex method for any component with an API version of 44.0 or later, annotate the Apex method with @AuraEnabled(cacheable=true).

    For example:

    @AuraEnabled(cacheable=true)
        public static Account getAccount(Id accountId) {
        // your code here
    }

    To update an existing component to API version 44.0, remove setStorable() calls in JavaScript code. Annotate the Apex method with @AuraEnabled(cacheable=true) instead of @AuraEnabled, or you get an error response for the action.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos