DEV Community

Cover image for How to cache requests in Angular

How to cache requests in Angular

Pedro Bruneli on February 23, 2024

Table contents Purpose What? How? Seeing the change Conclusion Purpose In our application, users often revisit the same pa...
Collapse
 
rajanchavda profile image
Rajan Chavda

How to clear cache at certain time interval ?

Collapse
 
pedrobruneli profile image
Pedro Bruneli • Edited

In that case you can do the share method.
e.g:

this.httpClient
      .get<Facts[]>('https://cat-fact.herokuapp.com/facts')
      .pipe(
        share({
          connector: () => new ReplaySubject(1),
          resetOnComplete: () => timer(5000),
          resetOnError: true,
}),
);
Enter fullscreen mode Exit fullscreen mode

We use the replaySubject as connector.
If api throws an error we reset it immediatly, otherwise we wait the timer subject to finish and resets it!

Regards