Skip to content

Commit 7987773

Browse files
authored
fix: 🐛 reduce farms' cache time to 20s (#11234)
<!-- Before opening a pull request, please read the [contributing guidelines](https://github.com/pancakeswap/pancake-frontend/blob/develop/CONTRIBUTING.md) first --> <!-- start pr-codex --> --- ## PR-Codex overview This PR introduces caching for farm data fetching in the API handler, improving performance by reducing repeated data fetching. It also updates the method of retrieving farm configuration to utilize the cache. ### Detailed summary - Added import for `cacheByLRU` from `@pancakeswap/utils/cacheByLRU`. - Created `_fetchFarmData` function to fetch and format farm data. - Implemented `fetchFarmData` using `cacheByLRU` for caching. - Replaced direct fetching of farm configuration with `fetchFarmData`. - Fixed `lastUpdatedAt` to correctly call `toISOString()` method. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent 2e46e4f commit 7987773

File tree

1 file changed

+17
-4
lines changed
  • apps/web/src/pages/api/configs/farms/v2

1 file changed

+17
-4
lines changed

Diff for: apps/web/src/pages/api/configs/farms/v2/index.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,34 @@ import {
33
fetchAllUniversalFarms,
44
formatUniversalFarmToSerializedFarm,
55
} from '@pancakeswap/farms'
6+
import { cacheByLRU } from '@pancakeswap/utils/cacheByLRU'
67
import { NextApiHandler } from 'next'
78
import { stringify } from 'viem'
89

10+
const _fetchFarmData = async (includeTestnet: boolean) => {
11+
const fetchFarmConfig = await fetchAllUniversalFarms()
12+
const farmConfig = includeTestnet ? [...fetchFarmConfig, ...UNIVERSAL_FARMS_WITH_TESTNET] : fetchFarmConfig
13+
return formatUniversalFarmToSerializedFarm(farmConfig)
14+
}
15+
16+
const fetchFarmData = cacheByLRU(_fetchFarmData, {
17+
ttl: 20_000,
18+
key: (params) => {
19+
return [params[0]]
20+
},
21+
})
22+
923
const handler: NextApiHandler = async (req, res) => {
1024
try {
11-
const fetchFarmConfig = await fetchAllUniversalFarms()
1225
const includeTestnet = !!req.query.includeTestnet
13-
const farmConfig = includeTestnet ? [...fetchFarmConfig, ...UNIVERSAL_FARMS_WITH_TESTNET] : fetchFarmConfig
14-
const legacyFarmConfig = await formatUniversalFarmToSerializedFarm(farmConfig)
26+
const legacyFarmConfig = await fetchFarmData(includeTestnet)
27+
1528
// cache for long time, it should revalidate on every deployment
1629
res.setHeader('Cache-Control', `max-age=300, s-maxage=300`)
1730

1831
return res.status(200).json({
1932
data: JSON.parse(stringify(legacyFarmConfig)),
20-
lastUpdatedAt: new Date().toISOString,
33+
lastUpdatedAt: new Date().toISOString(),
2134
})
2235
} catch (error) {
2336
console.error(error)

0 commit comments

Comments
 (0)