Caching data at the client side can significantly reduce the number of server round-trips and improve the performance of your Lightning components. Server actions support optional client-side caching, and the Lightning Data Service is built on top of a sophisticated client caching mechanism.
In Lightning terminology, a server action is an Apex method that you invoke remotely from your Lightning Component. 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.
Thanks
In Lightning terminology, a server action is an Apex method that you invoke remotely from your Lightning Component. 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.
Storable actions are the only currently supported type of storage. Storable actions cache server action response values. The storage name must be actions. Caching is especially beneficial for high-performance, mostly connected applications operating over high latency connections, such as 3G networks When you initialize storage, you can configure the expiration time, which is the duration in seconds that an entry is retained in storage..
Using storable actions, the cache behavior is controlled by two parameters set internally in the framework :
- Expiration age: This is the age of the cached response. Whenever the response is older then the existing one and same vice versa. Expiration age is currently set to 900 seconds in Salesforce lightning.
- Refresh age: Refresh age is the age of the response, when it gets refreshed, if the response is newer then the existing one, it will override the response, but lightning also calls the server method and get the response, if it different then the existing then it will override it as well. Refresh age is 30 second in lightning experience.
what to cache ?
The caching of the data should be decided on what is the response of your action, there are two type of action, one which returns everytime same thing, and another one is which is doesn't return the same response or dynamic.
The method which returns the same response each time called should be cached and other not,The setStorable function takes an optional argument, which is a configuration map of key-value pairs representing the storage options and values to set.
Cache Miss
If the action is not a cache hit as it doesn’t match a storage entry:
- The action is sent to the server-side controller.
- If the response is SUCCESS, the response is added to storage.
- The callback in the client-side controller is executed.
Cache Hit
If the action is a cache hit as it matches a storage entry:
- The callback in the client-side controller is executed with the cached action response.
- If the response has been cached for longer than the refresh time, the storage entry is refreshed. When an application enables storable actions, a refresh time is configured. The refresh time is the duration in seconds before an entry is refreshed in storage. The refresh time is automatically configured in Lightning Experience and the Salesforce mobile app.
- The action is sent to the server-side controller.
- If the response is SUCCESS, the response is added to storage.
- If the refreshed response is different from the cached response, the callback in the client-side controller is executed for a second time..
How to enable Storage Actions ?
To configure client-side storage for your standalone app, use <auraStorage:init> in the auraPreInitBlock attribute of your application’s template. For example:.
<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>
Thanks