Skip to content

Conversation

@bakoushin
Copy link
Contributor

@bakoushin bakoushin commented Sep 17, 2025

Description

Cache base tokens info for 5 seconds, while allowing stale-while-revalidate behavior.

This will ensure:

  1. Data is always fresh. 5 second lag seems negligible, given the direct request may take 1-3 seconds
  2. Data is retrieved fast

@bakoushin bakoushin changed the title chore: cache getTokensInfo chore: cache getTokensInfoWithPrices Sep 17, 2025
// 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
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Member

@jeanregisser jeanregisser left a 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.

@bakoushin
Copy link
Contributor Author

@jeanregisser well, the responses should contain actual prices, so probably the server shouldn't recommend caching them

): Promise<TokensInfo> {
// Get base tokens
const url = networkIds.length
? `${getTokensInfoUrl}?networkIds=${networkIds.join(',')}`
Copy link
Contributor

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

@bakoushin bakoushin enabled auto-merge (squash) September 19, 2025 08:55
@bakoushin bakoushin merged commit 504666a into main Sep 19, 2025
6 checks passed
@bakoushin bakoushin deleted the alex/cache-tokens-info branch September 19, 2025 08:58
bakoushin added a commit that referenced this pull request Sep 19, 2025
### 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.
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

Successfully merging this pull request may close these issues.

4 participants