Skip to content

Commit

Permalink
feat: add-caching-api-level
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolin committed Jan 9, 2025
1 parent 5b48c33 commit b02eaf2
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
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'
1 change: 1 addition & 0 deletions 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
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26066,6 +26066,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

0 comments on commit b02eaf2

Please sign in to comment.