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

feat: add caching at api level #5266

Open
wants to merge 1 commit into
base: remove_api_mock
Choose a base branch
from

Conversation

anxolin
Copy link
Contributor

@anxolin anxolin commented Jan 1, 2025

Summary

Native price is being used in several places of the app. Mostly for limit orders to get the market price, and for getting USD price.

I saw that, on load the native price for some tokens was being fetched at least 6 times.

BEFORE:
image

AFTER:
image

Context

One of the reasons we fetch so many times, is because we fetch it for multiple markets, and there's some repetition in the tokens of the markets. It does 1 call per token.

image

One easy way to fix, its to provide a cached version of the getNativePrice in apps/cowswap-frontend/src/api/cowProtocol/api.ts

Normally we add caching either:

  • using SWR (which only supports using hooks)
  • Or we use joitai, which also is tailored for hooks
  • or we use some kind of static state with some update logic using timers. Normally using hooks too.

One problem with these approaches, is that api is not a hook. They are pure functions.
In this case, If we wanted to use caching using hooks it would be more complicated, because we need to get an instance of the fetch function in other hooks or components and pass it. Native price is being used in multiple place, so it seems too complicated.

Quick LRU

I added a convenient library to add a simple and lightweight LRU cache (3.2K)

Screenshot at Jan 01 17-11-31

Generic caching tool

I implemented a generic fetchWithCache which makes use of quick LRU caches.

This utility, will cache promises and not the resolved object. With this, we can make sure the native price is only fetched once. If we cached the resolved object, it would fetch 6 times on load for the same token, because the app would try to fetch in parallel the same token multiple times, and the result would not be in the cache.

Caching the response allow us to only fetch it once per TTL interval.

Next PRS

One thing I would like to do, is to use this solution as just a temporary one, and implement all the refreshing logics in the SDK. I have some ideas, but its completely out of the scope of this PR.

Copy link

vercel bot commented Jan 1, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
cosmos ✅ Ready (Inspect) Visit Preview Jan 1, 2025 4:27pm
cowfi ✅ Ready (Inspect) Visit Preview Jan 1, 2025 4:27pm
explorer-dev ✅ Ready (Inspect) Visit Preview Jan 1, 2025 4:27pm
sdk-tools ✅ Ready (Inspect) Visit Preview Jan 1, 2025 4:27pm
swap-dev ✅ Ready (Inspect) Visit Preview Jan 1, 2025 4:27pm
widget-configurator ✅ Ready (Inspect) Visit Preview Jan 1, 2025 4:27pm

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.

1 participant