isaacs / node-lru-cache

A fast cache that automatically deletes the least recently used items
http://isaacs.github.io/node-lru-cache/
ISC License
5.38k stars 353 forks source link

document that sizeCalculation can be called with a Promise when using fetch() #267

Closed nornagon closed 1 year ago

nornagon commented 1 year ago

I was tripped up by the fact that the sizeCalculation method is called first with a Promise and then later with the resolved value when using fetch().

First here, calling with the Promise: https://github.com/isaacs/node-lru-cache/blob/724ee04e10a23a54c55b300e39e315cd75d8e0da/index.js#L758

and then here, calling with the value once the promise is resolved: https://github.com/isaacs/node-lru-cache/blob/724ee04e10a23a54c55b300e39e315cd75d8e0da/index.js#L731

and, further, sizeCalculation must return a valid size for the Promise, which is impossible in my use case (I don't know the size of the data before I fetch it!) and I suspect in many cases.

I ended up making my sizeCalculation function return 1 when the value it's passed is instanceof Promise, but that feels like quite a hack, and took me a while to arrive at.

isaacs commented 1 year ago

Haha yeah, that's a bug, those promises should be internal only, and never leaked to user code.

nornagon commented 1 year ago

Ah, I thought it might be! Makes sense, thanks :)

isaacs commented 1 year ago

Hey, what version are you using? It looks like this was already fixed on f351e681, which was published as 7.14.1.

nornagon commented 1 year ago

ah, i'm using 7.14.0. Seems to be fixed in 7.14.1, thanks!