alfonsusac / nextjs-better-unstable-cache

Wrapper function for unstable_cache() from next/cache (Next.js Caching)
https://www.npmjs.com/package/nextjs-better-unstable-cache
72 stars 5 forks source link

Memoization is only HIT on first function call #1

Closed MaKTaiL closed 1 year ago

MaKTaiL commented 1 year ago

image

image

Shouldn't it be the other way around? Memoization should MISS on first call and HIT on further ones.

andrewkucz commented 1 year ago

Figured Id ask here since its related to what you brought up: but what does the "Memoization" log mean exactly?

Is it when it is actually called & stored for the first time? If so I could see it being a HIT on the first and then a MISS the future ones? Or rather maybe it would make sense for the future ones to not log anything for that? Not sure

MaKTaiL commented 1 year ago

Figured Id ask here since its related to what you brought up: but what does the "Memoization" log mean exactly?

Is it when it is actually called & stored for the first time? If so I could see it being a HIT on the first and then a MISS the future ones? Or rather maybe it would make sense for the future ones to not log anything for that? Not sure

Memoization is when the function is stored on the first call and any time a new page or component calls the same function its result is reused instead of being called again. In my opinion it would make more sense for it to HIT on further calls since HIT means you found a valid memoization to be used.

andrewkucz commented 1 year ago

Figured Id ask here since its related to what you brought up: but what does the "Memoization" log mean exactly? Is it when it is actually called & stored for the first time? If so I could see it being a HIT on the first and then a MISS the future ones? Or rather maybe it would make sense for the future ones to not log anything for that? Not sure

Memoization is when the function is stored on the first call and any time a new page or component calls the same function its result is reused instead of being called again. In my opinion it would make more sense for it to HIT on further calls since HIT means you found a valid memoization to be used.

Wouldn't that make it equivalent to the Data Cache log event then?

What would an example be where Data Cache is HIT and memoization is a MISS (by your definition)

MaKTaiL commented 1 year ago

Figured Id ask here since its related to what you brought up: but what does the "Memoization" log mean exactly? Is it when it is actually called & stored for the first time? If so I could see it being a HIT on the first and then a MISS the future ones? Or rather maybe it would make sense for the future ones to not log anything for that? Not sure

Memoization is when the function is stored on the first call and any time a new page or component calls the same function its result is reused instead of being called again. In my opinion it would make more sense for it to HIT on further calls since HIT means you found a valid memoization to be used.

Wouldn't that make it equivalent to the Data Cache log event then?

What would an example be where Data Cache is HIT and memoization is a MISS (by your definition)

Data cache and memoization can both HIT at the same time. First the memoization will HIT, it found a valid memoization, then it will HIT the valid cache. I just think it makes more sense. It is up to the owner to decide what's best.

alfonsusac commented 1 year ago

Shouldn't it be the other way around? Memoization should MISS on first call and HIT on further ones.

React's Cache should MISS for the first time regardless the Data Cache is HIT or MISS. Its independent of each other.

Is it when it is actually called & stored for the first time? If so I could see it being a HIT on the first and then a MISS the future ones? Or rather maybe it would make sense for the future ones to not log anything for that? Not sure

Yes, but I'd personally just disable the "Memoization" Log since its purely demonstrative, for learning purposes.

The ideal log should look like this Data Cache - HIT Memoization - HIT Memoization - MISS Memoization - MISS Memoization - MISS

and Data Cache - MISS Memoization - HIT Memoization - MISS Memoization - MISS Memoization - MISS

What would an example be where Data Cache is HIT and memoization is a MISS (by your definition)

This should never happen as the Data Cache is triggered inside Memoization Cache so in order for the Data Cache to Log is to HIT the memoization.

The mechanism is still correct though I just happen to mix up HIT with MISS.