-
Notifications
You must be signed in to change notification settings - Fork 11
chore: cache getTokensInfoWithPrices
#820
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
Conversation
getTokensInfogetTokensInfoWithPrices
src/runtime/getPositions.ts
Outdated
| // We don't have this info here but it's not yet needed for base tokens anyway | ||
| balance: toDecimalNumber(0n, 0), | ||
| totalSupply: toDecimalNumber(0n, 0), | ||
| let tokensInfo = tokensInfoCache.get(TOKENS_INFO_CACHE_KEY) as TokensInfo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cache key should include network ids. Otherwise w/ different apps calling with different network ids, we'd return the wrong list of tokens.
Also, if we just want caching for the usdc dapp, we could pass an optional query parameter and only cache if its set. Would also solve the above problem as network ids from the dapp won't change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great catch, thanks!
I refactored it to stale-while-revalidate behavior with only 5 seconds TTL
it think it should be beneficial for any app, as it allows faster response times with a negligible tradeoff on freshness
jeanregisser
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about setting needed cache headers at the endpoint producing these values?
The got client would respect that out of the box, If I remember correctly.
But if we want an in-memory cache, got also supports it.
|
@jeanregisser well, the responses should contain actual prices, so probably the server shouldn't recommend caching them |
src/runtime/getPositions.ts
Outdated
| ): Promise<TokensInfo> { | ||
| // Get base tokens | ||
| const url = networkIds.length | ||
| ? `${getTokensInfoUrl}?networkIds=${networkIds.join(',')}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can also sort network ids to ensure consistent keys
### Description Cache Beefy API requests using stale-while-revalidate behavior, similar to #820 It will ensure that the cached result is always prioritized, while keeping cache updated every 5 seconds.
Description
Cache base tokens info for 5 seconds, while allowing stale-while-revalidate behavior.
This will ensure: