medikoo / memoizee

Complete memoize/cache solution for JavaScript
ISC License
1.73k stars 61 forks source link

maxAge: 0 should disable the cache #78

Open Junkern opened 7 years ago

Junkern commented 7 years ago

When using memoizee, I inject a config variable as the value for maxAge so I can use different values for different environments (prod, dev, testing...).

When setting maxAge: 0 I would expect that the cache is disabled, as every value is cached for 0 milliseconds.. This seems not to be the case, however.

More information, if needed: On production I use memoizee to minimize calls to other services, to reduce the load. For testing, I am using nock to mock HTTP responses. At the end of ever test I am verifying if all mocked calls finished successfully. I want that every mocked HTTP response is called, so I want to disable the cache,.

I could introduce a environment flag and then decide based on that whether I memoizee a function or not. But I think maxAge: 0 would be more elegant.

medikoo commented 7 years ago

Thanks for reporting, interesting use case. Currently this setting follows setTimeout behavior. So 0 delays cache purge until callback in setTimeout(fn, 0) is released.

Still I agree that it might be nicer, if it just disables the cache in such case

Junkern commented 7 years ago

Thanks for the quick response!

I still have one question:

So 0 delays cache purge until callback in setTimeout(fn, 0) is released

Do you mean, that the cache is purged after the callback is finished? (So after the memoizeed function has finished its first call?)

medikoo commented 7 years ago

Do you mean, that the cache is purged after the callback is finished? (So after the memoizeed function has finished its first call?)

Sorry, no, in case of 0 whole maxAge setting is ignored. However if you pass 1 you'll have cached purged shortly after call.

Junkern commented 7 years ago

Makes totally sense. I also have looked into the code and seen the if (!maxAge) return; but I thought maybe the line before (maxAge = timeout(maxAge);) introduces some magic.

I am passing 1 for now, but I would be happy if 0will disable the cache:)