Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow me to control the cache time based on the fresh value #25

Closed
kentcdodds opened this issue Jan 18, 2023 · 6 comments
Closed

Allow me to control the cache time based on the fresh value #25

kentcdodds opened this issue Jan 18, 2023 · 6 comments

Comments

@kentcdodds
Copy link
Member

Related to my use case in #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?

@Xiphe
Copy link
Collaborator

Xiphe commented Jan 19, 2023

Sounds like a useful addition! I'm gonna have a look into this within the next days

Xiphe added a commit that referenced this issue Jan 19, 2023
in order to support dynamically changing chage properties while getting the value

fix #25
@Xiphe
Copy link
Collaborator

Xiphe commented Jan 19, 2023

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'
  }
});

@kentcdodds
Copy link
Member Author

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?

@Xiphe
Copy link
Collaborator

Xiphe commented Jan 20, 2023

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 #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.

@kentcdodds
Copy link
Member Author

Just tried it out and it's great 👍 Thanks!

@Xiphe Xiphe closed this as completed in 9c53976 Jan 25, 2023
@Xiphe
Copy link
Collaborator

Xiphe commented Jan 25, 2023

Awesome! It's released now under v3.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants