How to cache an observable data in Angular 2 ?

Member

dejon

by dejon , in category: Technology , 2 months ago

How to cache an observable data in Angular 2 ?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by adolf , 2 months ago

Caching of an observable data is done with the help of “observable.cache”. We can use caching in order to cache the response in the memory and then, on the next subscription, instead of requesting the remote server again. This operator is used at the end of the string. Caching is important for the performance, especially on bandwidth restricted devices and slow networks. You should have a good understanding of caching while working with promises but while translating it to observable, it is a bit difficult. Therefore, when interacting with observables, we typically set up a subscription on the consumer side and react to values coming through the pipe. We can easily add caching to the observables by adding publishReplay(1) and refCount.