0

I want to be able to cache the server response received in one Salesforce lightning component on page 1 and later be able to use the response on page 2 component.

I was reading through using lightning template Storable actions. I do not know how to store the response and retrieve the response in another page. Any directions on this will be helpful. Thanks for your time and help.

0

Below is a sample function which makes a server call from Lightning components. if you look at the line highlighted below in yellow thats the place where we are caching the response. Now subsequent requests for the same server method with the same set of arguments can be accessed from that cache.

Using storable actions, the cache behavior is controlled by two parameters set internally in the framework :

Expiration age: maximum age of the cached response. The cached response is discarded if it is older than the expiration age. In Lightning Experience, the expiration age is currently set to 900 seconds. This value is subject to change, and you can’t change it yourself.

Refresh age: maximum age for the cached response to be considered “fresh.” If the cached response is older than the refresh age (and younger than the expiration age), it is provided to the client, but the framework also invokes the server method in the background to refresh the cache. If the new response is different from the cached response, the action callback function is called a second time. In Lightning Experience, the refresh age is currently set to 30 seconds. Like the expiration age, this value is subject to change, and can’t be changed.

var action = cmp.get("c.serverEcho");
    action.setParams({ firstName : cmp.get("v.firstName") });

// Create a callback that is executed after 
// the server-side action returns
action.setCallback(this, function(response) {
    var state = response.getState();
    if (state === "SUCCESS") {
        // Alert the user with the value returned 
        // from the server
        alert("From server: " + response.getReturnValue());

        // You would typically fire a event here to trigger 
        // client-side notification that the server-side 
        // action is complete
    }
    else if (state === "INCOMPLETE") {
        // do something
    }
    else if (state === "ERROR") {
        var errors = response.getError();
        if (errors) {
            if (errors[0] && errors[0].message) {
                console.log("Error message: " + 
                         errors[0].message);
            }
        } else {
            console.log("Unknown error");
        }
    }
});

code highlighted below which sets storable

action.setStorable();//storing the response

$A.enqueueAction(action);
1
  • I did not realize that it was caching the action with same parameters. – Raja Vivekanandhan Jun 28 '17 at 3:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.