Closed kentcdodds closed 1 year ago
Sounds like a useful addition! I'm gonna have a look into this within the next days
Could you give cachified@3.1.0-context-info.1
a try?
You can now patch the cache metadata during the getFreshValue call.
cachified({
/* ... */
ttl: 5,
getFreshValue({ metadata }) {
metadata.ttl = 10;
return 'value'
}
});
Thanks for working on this so quickly!
Hmmm... Interesting choice. Is there a reason you didn't go with making ttl
a function that accepts the latest value as I suggested?
Yeah Multiple reasons.
First and foremost doing it this way was a tiny change
Secondly: The internal pending value cache also uses the ttl. This would have lead to a situation where a ttl-function would be called once with no value to determine the time other calls should wait for the same pending value and a second time once we have the value. Currently this system is transparent to the users and with this change users would need to know about it in order to understand why the ttl-function is called multiple times.
Last but not least this might solve cases like https://github.com/Xiphe/cachified/issues/16 (cc @garand) where by overwriting the createdTime
we could dynamically increasing the ttl by the time it took to get a fresh value.
The approach might be a little more imperative then what you proposed but I like that in this case. In my mind it feels like initiating the call with a base set of assumptions and then while getting the fresh value we gain more knowledge and fine-tune some of our assumptions based on that.
Just tried it out and it's great 👍 Thanks!
Awesome! It's released now under v3.1.0
Related to my use case in https://github.com/Xiphe/cachified/issues/24, if the user has a gravatar, then I'm happy to let that cache hang around for days. If they don't then I want to refresh it every 20 seconds. Maybe the
ttl
could accept a function which passes the current value if it's available and undefined if not?