diff --git a/package.json b/package.json index 680f1016..83fcc817 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "jsonpath": "^1.1.1", "millify": "^6.1.0", "next": "13.4.8", + "node-cache": "^5.1.2", "numeral": "2.0.6", "prop-types": "^15.7", "react": "18.2.0", diff --git a/pages/api/integrator.ts b/pages/api/integrator.ts index 0b5ce40a..23dc4f1d 100644 --- a/pages/api/integrator.ts +++ b/pages/api/integrator.ts @@ -1,6 +1,9 @@ import axios from 'axios'; import type { NextApiRequest, NextApiResponse } from 'next'; import { IntegratorsVolumeResponse } from 'src/typings'; +import NodeCache from 'node-cache'; + +const cache = new NodeCache({ stdTTL: 3600 }); const INTEGRATORS_VOLUME_URL = 'https://api.dune.com/api/v1/query/2647536/results'; @@ -41,8 +44,16 @@ export default async function handler( res: NextApiResponse, ) { try { + const cacheKey = 'integratorsVolume'; + const cachedData = cache.get(cacheKey); + + if (cachedData) { + res.status(200).json({ result: cachedData }); + return; + } + const result = await fetchDuneData(); - res.setHeader('Cache-Control', 's-maxage=3600'); + cache.set(cacheKey, result, 3600); res.status(200).json({ result }); } catch (err) { res.status(500).json({ error: 'failed to load data' }); diff --git a/pages/index.tsx b/pages/index.tsx index 90852c07..60044250 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -243,7 +243,7 @@ export const getStaticProps: GetStaticProps = async () => { markets, uniqueTradingAccounts, }, - revalidate: 60 * 10, + revalidate: 60 * 60, }; } catch (e) { console.log(e); @@ -259,7 +259,7 @@ export const getStaticProps: GetStaticProps = async () => { markets: null, uniqueTradingAccounts: null, }, - revalidate: 5, + revalidate: 60 * 5, }; } }; diff --git a/yarn.lock b/yarn.lock index abddc45a..95e6b33f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8438,7 +8438,7 @@ __metadata: languageName: node linkType: hard -"clone@npm:^2.0.0, clone@npm:^2.1.1": +"clone@npm:2.x, clone@npm:^2.0.0, clone@npm:^2.1.1": version: 2.1.2 resolution: "clone@npm:2.1.2" checksum: aaf106e9bc025b21333e2f4c12da539b568db4925c0501a1bf4070836c9e848c892fa22c35548ce0d1132b08bbbfa17a00144fe58fccdab6fa900fec4250f67d @@ -16801,6 +16801,15 @@ __metadata: languageName: node linkType: hard +"node-cache@npm:^5.1.2": + version: 5.1.2 + resolution: "node-cache@npm:5.1.2" + dependencies: + clone: 2.x + checksum: b0bdd81a6fee4754fb984a05246b510bb35dc54721116d465899bf4229ee3287fdafb47da526900ee9924fb402ed5c7d8050049d37d8bf2d26dbafc23a2c3205 + languageName: node + linkType: hard + "node-fetch@npm:2.6.7, node-fetch@npm:^2.6.0, node-fetch@npm:^2.6.1, node-fetch@npm:^2.6.7": version: 2.6.7 resolution: "node-fetch@npm:2.6.7" @@ -20728,6 +20737,7 @@ __metadata: millify: ^6.1.0 next: 13.4.8 next-sitemap: 1.3.2 + node-cache: ^5.1.2 numeral: 2.0.6 prettier: ^2.8.8 prop-types: ^15.7