angelnikolov / ts-cacheable

Observable/Promise Cache Decorator
https://npmjs.com/package/ts-cacheable
ISC License
340 stars 42 forks source link

Refresh cache #39

Closed leocharrua closed 5 years ago

leocharrua commented 5 years ago

Hello.

Thanks for his work.
How do you clear (refresh) the cache programmatic?

Thanks

angelnikolov commented 5 years ago

Hello @leocharrua, you can use @CacheBuster as specified in the docs. Let me know if I can help further.

leocharrua commented 5 years ago

Hi. Sorry, I don't understand how I must use that decorator. Can you give me a example code? Thanks

leocharrua commented 5 years ago

Sorry, can you help me? I put the @cachebuster in the function ... then? How I clear the cache (allow to call the http.post)? Thanks again.

angelnikolov commented 5 years ago

You can decorate your post method (the method which will save some data to the server and i.e - need to clear the cache). You need to provide a subject to it which will be next'ed when your save request completes. Then you also provide the very same subject to the Cacheable decorator on your data-fetching method, so it knows when to clear its cache. In short, when you call saveData and it completes, all cache of getData will be cleared.

If you want more control on when to clear the getData cache, you can just call saveDataSubject.next() on your own. You can even export the subject and use it anywhere else.

const saveDataSubject = new Subject();
@CacheBuster(
  {
     cacheBusterNotifier: saveDataSubject
  }
)
saveData(){}
@Cacheable(
  {
     cacheBusterObserver: saveDataSubject.asObservable()
  }
)
getData(){}
leocharrua commented 5 years ago

Thank you very much!!!