rxjs http request is not cached

Arikael

I'm trying to understand rxjs publishReplay, refCount and share What I wanted to achieve was caching of a http request as described here What is the correct way to share the result of an Angular Http network call in RxJs 5?

Here's what I came up with

export class TestService {

  constructor(private http: HttpClient) {}
  getData(): Observable<any> {
    return this.http.get('https://httpbin.org/anything').map((data: any) => {
    console.log('getting data');

      return Math.floor(Math.random() * 20);
    }).publishReplay(1).refCount();
  } 
}

calling it

t.getData().subscribe((data: any) => console.log(data));
t.getData().subscribe((data: any) => console.log(data));

the request will usually made twice.

Just to unterstand the cause of this:

Am I correct that this behavior occurs if the observable (http request) hasn't completed yet when I subscribe a second time?

If I move the seccond t.getData() inside the the first subscriptions next only one request is mode.

here is a stackblitz with the code

pixelbits

Every time you call the method getData() it is returning a new observable - it doesn't matter that you've converted a cold observable to a hot one.

A more appropriate use of .publishReplay is to keep the same instance of the Observable.

For example:

export class TestService {
  data: Observable<any>;
  constructor(private http: HttpClient) {
     this.data = this.getData();
  }
  getData(): Observable<any> {
    return this.http.get('https://httpbin.org/anything').map((data: any) =>{
    console.log('getting data');

      return Math.floor(Math.random() * 20);
    }).publishReplay(1).refCount();
  } 
}

Calling it:

this.data.subscribe(t=> ...);
this.data.subscribe(t=> ...);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Updating cached http request in Angular with RxJS and shareReplay

RxJS queue http request

RxJs combine http request

Nested http request in RxJS

Re-issueing a cached (shareReplay) HTTP request?

HTTP 504 Unsatisfiable Request (only-if-cached)

Angular RxJS Subscribe to HTTP Request in HTTP Request

RxJS HTTP request error handling

Retrofit+Okhttp.HTTP 504 Unsatisfiable Request (only-if-cached)

Angular 2+ cached HTTP request force reload

Angular 6 multiple HTTP request RxJs

http get request with array using rxjs

prevent multiple http request rxjs on route

Angular RXJS calling http post request is not working

Perform http request for every item in a previous request response with Rxjs

angularjs: $http is cached

How to retry a http request until a condition is met using rxjs

Cancel only specific pending HTTP request with RXJS switchMap

Angular 12 RxJs BehaviourSubject and HTTP Get request issue

Rxjs with Axios Http Get, execute code after request finished

How cancel angular http request made in RXJS Effects

How can I make multiple nested http request in RxJs?

Angular 7 Rxjs - In-Flight HTTP request not cancelling for subscription

How to delay retrying to send an HTTP request with RxJS5?

Shortest code to cache Rxjs http request while not complete?

How to cancel a pending request in an inner HTTP observable with RXJS?

Rxjs angular 6/7 mergeMap delay http request

Make an http request inside error handling Angular 7 RXJS

How can I prevent my HTTP Get request from being cached in Swift?