ngneat / cashew

🐿 A flexible and straightforward library that caches HTTP requests in Angular
https://www.netbasal.com
MIT License
682 stars 33 forks source link

docs: cache then refresh data #94

Open leogouveia opened 12 months ago

leogouveia commented 12 months ago

I'm submitting a...

[x] Documentation issue or request

Current behavior

Cashew caches HTTP requests and only updates the cached data when it is deleted/expired.

Expected behavior

Cashew retrieves cached data, returns it to the user, and then makes an HTTP request to update the cached data. Once the updated data is available, Cashew pushes it to the user.

What is the motivation / use case for changing the behavior?

I am using @ngneat/cashew to cache HTTP requests in my Angular project.

I would like to know if it is possible to implement the cache and refresh strategy, as exemplified on the angular.io page link

I tried looking for information in the documentation and code, but I could not find any.

// cache-then-refresh
if (req.headers.get('x-refresh')) {
  const results$ = sendRequest(req, next, this.cache);
  return cachedResponse ?
    results$.pipe( startWith(cachedResponse) ) :
    results$;
}
// cache-or-fetch
return cachedResponse ?
  of(cachedResponse) : sendRequest(req, next, this.cache);

Environment


Angular version: 15.2.2


Browser:
- [x] Edge version 118

For Tooling issues:
- Node version: v18.17.0  
- Platform: Windows