angelnikolov / ts-cacheable

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

How to cache GET/POST etc for multible critera #5

Closed alindberg closed 6 years ago

alindberg commented 6 years ago

I would like to cache the following example:

/service/key?=1 /service/key?=2

Etc. Additional requests to the above keys would returned from cache. Keys not matching would be returned from the HTTPClient with the results being added to the cache.

The data requests could be from either Get or Post.

Thank you for your guidance.

angelnikolov commented 6 years ago

@alindberg You don't cache the requests, but you cache methods which return Observables. So you can create a method which takes the key as a parameter, and that will be used for caching. Then when you decorate the method with @Cacheable, you can just add the maxCacheCount parameter to it to specify how many unique parameters method invocations should be cached. If you decorate your method with something like:

@Cacheable({
    maxCacheCount: 2
})
getData(key: string){}

Both your requests will be essentially cached.