angelnikolov / ts-cacheable

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

RFC: Modifiable cache experimentation. #97

Closed angelnikolov closed 3 years ago

angelnikolov commented 3 years ago

For all who wanted a way to modify cache, now you have it. Let me know if you like the API. Basically, you'd use it like:

const cacheModifier = new Subject<any>();
  @Cacheable({
    storageStrategy: InMemoryStorageStrategy,
    cacheModifier
  })
  getMutableData(parameter: string) {
    return this.mockServiceCall(parameter);
}

Then, when you want to modify the cache, you'd emit through cacheModifier a callback:

cacheModifier.next((allCachedItems: any[]) => {
   const specificCachedItem = allCachedItems.find(cacheItem => cacheItem.parameters[0] === 'whatever...).response.payload = 'modified item';
    return allCachedItems;
});

The return value of this callback will be automatically stored for you in the cache, against the cacheKey of this method. cc. @matthew2564 @dmbaker90 @Stefan-Kosker @muratcorlu @atmdev Ref: https://github.com/angelnikolov/ts-cacheable/issues/95, https://github.com/angelnikolov/ts-cacheable/issues/45, https://github.com/angelnikolov/ts-cacheable/issues/83, https://github.com/angelnikolov/ts-cacheable/issues/94

codecov[bot] commented 3 years ago

Codecov Report

Merging #97 (4bc712e) into master (78cf9ff) will decrease coverage by 2.18%. The diff coverage is 54.54%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #97      +/-   ##
==========================================
- Coverage   84.64%   82.45%   -2.19%     
==========================================
  Files          12       12              
  Lines         267      285      +18     
  Branches       66       70       +4     
==========================================
+ Hits          226      235       +9     
- Misses         31       41      +10     
+ Partials       10        9       -1     
Impacted Files Coverage Δ
common/IAsyncStorageStrategy.ts 100.00% <ø> (ø)
common/IStorageStrategy.ts 100.00% <ø> (ø)
common/DOMStorageStrategy.ts 28.88% <16.66%> (-1.89%) :arrow_down:
common/LocalStorageStrategy.ts 80.00% <16.66%> (-9.75%) :arrow_down:
cacheable.decorator.ts 98.33% <100.00%> (+1.78%) :arrow_up:
common/InMemoryStorageStrategy.ts 100.00% <100.00%> (ø)
promise.cacheable.decorator.ts 94.28% <100.00%> (+0.16%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 78cf9ff...4bc712e. Read the comment docs.

Stefan-Kosker commented 3 years ago

Damn dude seriously, you are a beast! I wished, many other open source library developers would take an example from you! Seriously.

I have one question: I just don't get where this cacheModifier method gets called. Or how I can access to it. Maybe I just dont understand it because it is a bit late. But would still be thankful for an answer.

angelnikolov commented 3 years ago

@Stefan-Kosker Thanks for the kind words! So basically, you'd use it similarly to the cacheBusterNotifier. You create a plain subject somewhere in your code (you can also export it from a common place or whatever), like:

const cacheModifier = new Subject();

Then you tell the @Cacheable decorator, that you want to use this cacheModifier subject to tell the cache when to update and how to update. When and How are important, because whatever callback you emit through the Subject will be called with your cache as the parameter and return the modified version. Basically cacheModifier.next(cache => {modify the cache here... return cache;}); Let me know if you got more questions.