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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions apps/cowswap-frontend/src/api/cowProtocol/apiCached.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import ms from 'ms.macro'
import * as api from './api'
import QuickLRU from 'quick-lru'
import { fetchWithCache } from '@cowprotocol/common-utils'

import { NativePriceResponse, SupportedChainId as ChainId } from '@cowprotocol/cow-sdk'

export * from './api'

const nativePriceCache = new QuickLRU<string, Promise<NativePriceResponse>>({
maxSize: 1000,
maxAge: ms`1m`,
})

export async function getNativePrice(chainId: ChainId, currencyAddress: string): Promise<NativePriceResponse> {
return fetchWithCache({
key: `${chainId}:${currencyAddress}`,
fetch: () => api.getNativePrice(chainId, currencyAddress),
cache: nativePriceCache,
})
}
2 changes: 1 addition & 1 deletion apps/cowswap-frontend/src/api/cowProtocol/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './api'
export * from './apiCached'
19 changes: 19 additions & 0 deletions libs/common-utils/src/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import QuickLRU from 'quick-lru'

export interface GetAndCacheParams<T> {
key: string
fetch: () => Promise<T>
cache: QuickLRU<string, Promise<T>>
}

export async function fetchWithCache<T>({ key, fetch, cache }: GetAndCacheParams<T>): Promise<T> {
const cached = cache.get(key)
if (cached) {
return cached
}

const resultPromise = fetch()
cache.set(key, resultPromise)

return resultPromise
}
1 change: 1 addition & 0 deletions libs/common-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ export * from './userAgent'
export * from './getCurrencyAddress'
export * from './errorToString'
export * from './fetch'
export * from './cache'
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
"polyfill-object.fromentries": "^1.0.1",
"popper-max-size-modifier": "^0.2.0",
"qs": "^6.12.1",
"quick-lru": "^7.0.0",
"react": "19.0.0-rc-66855b96-20241106",
"react-appzi": "^1.0.4",
"react-confetti": "^6.1.0",
Expand Down Expand Up @@ -343,4 +344,4 @@
"vite-tsconfig-paths": "~4.3.2",
"vitest": "~0.32.0"
}
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26054,6 +26054,11 @@ quick-lru@^5.1.1:
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-5.1.1.tgz#366493e6b3e42a3a6885e2e99d18f80fb7a8c932"
integrity sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA==

quick-lru@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-7.0.0.tgz#447f6925b33ae4d2d637e211967d74bae4b99c3f"
integrity sha512-MX8gB7cVYTrYcFfAnfLlhRd0+Toyl8yX8uBx1MrX7K0jegiz9TumwOK27ldXrgDlHRdVi+MqU9Ssw6dr4BNreg==

rabin-wasm@^0.1.4:
version "0.1.5"
resolved "https://registry.yarnpkg.com/rabin-wasm/-/rabin-wasm-0.1.5.tgz#5b625ca007d6a2cbc1456c78ae71d550addbc9c9"
Expand Down
Loading