jaredwray/cacheable

Handling of timeToLive() returning floats

Closed this issue · 2 comments

I originally opened an issue on http-cache-semantics, but this can be fixed on here as well.
The full details can be viewed on the original issue i've opened kornelski/http-cache-semantics#35, but I'll copy it for your convenience.

״

It causes a problem in https://github.com/lukechilds/cacheable-request. Since this is the root cause of the problem, I opened the issue here.
Although milliseconds can have fractions, the fractions in this case are not caused by the division between the numbers, but by float arithmetic problems, it seems to me.

That's the error I'm getting in cacheable-request which is coming from Keyv I think:
Error [CacheError]: ERR value is not an integer or out of range

code snippet in cacheable-request:

let ttl = opts.strictTtl ? response.cachePolicy.timeToLive() : undefined;
if (opts.maxTtl) {
	ttl = ttl ? Math.min(ttl, opts.maxTtl) : opts.maxTtl;
}

await this.cache.set(key, value, ttl);

The program gets to this.cache.set() providing ttl = response.cachePolicy.timeToLive() = 1256996.9999999998 for example, and once set() is executed, the error above is thrown.

In order to trigger this behavior I'm setting private,max-age=4 on cache-control header in the request, max-age 4 can be changed to any other number that triggers this behavior.

״

@avivimgn - thanks for the heads up on this issue. At this time we are looking to get this project cleaned up first and will consider this issue later. Do you want to do a pull request around how to test for this and we will get it considered?

closing do to no response. happy to look at this again with a test case we can validate against