angelnikolov / ts-cacheable

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

Bust cache from a specific function only #78

Closed deyanpeychev00 closed 4 years ago

deyanpeychev00 commented 4 years ago

Really like your project and the fact that it's a Bulgarian one!

Just a question: Having read the documentation and found that the cache can be busted using:

import { globalCacheBusterNotifier } from './cacheable.decorator';

globalCacheBusterNotifier.next();

Is there a way that I can bust the cache only for a specific http request?

angelnikolov commented 4 years ago

Yes, you can provide a cacheBusterNotifier: subject in the @Cacheable config and next on it when you want a method's cache to be evicted. https://github.com/angelnikolov/ngx-cacheable#cache-busting

deyanpeychev00 commented 4 years ago

Just checked that, but I seem to get an error:

Argument of type '{ cacheBusterNotifier: Subject; }' is not assignable to parameter of type 'IObservableCacheConfig'. Object literal may only specify known properties, and 'cacheBusterNotifier' does not exist in type 'IObservableCacheConfig'.

Here is how I have implemented it:

const allUsersCacheBuster$ = new Subject<void>();
  @Cacheable({
    cacheBusterObserver: allUsersCacheBuster$
  })
  getAllUsers(query): Observable<HttpResponse<any>> {
    // gets data for users
  }
  @Cacheable({
    cacheBusterNotifier: allUsersCacheBuster$
  })
  createUser(userData): Observable<any> {
    // creates new user
  }
deyanpeychev00 commented 4 years ago

Update:

Ignore my comment with the error message, just made it work with removing the cacheBusterNotifier property from the @Cacheable() decorator and invoking allUsersCacheBuster$.next() in the methods that I want to clear the specific cache.

The issue can now be marked as closed or remain here if considered as helpful to someone. :)

dmbaker90 commented 3 years ago

Maybe this should be documented. I tried cacheBusterNotifier as well like the documentation and it didn't work.

Does not work:

@Cacheable({
   cacheBusterNotifier: allUsersCacheBuster$
  })
  createUser(userData): Observable<any> {
    // creates new user
    return EMPTY;
  }

Works:

  createUser(userData): Observable<any> {
    allUsersCacheBuster$.next();
    return EMPTY;
  }