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

Quickfix/better cache #252

Merged
merged 3 commits into from
Sep 20, 2023
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 12 additions & 1 deletion pages/api/integrator.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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' });
Expand Down
4 changes: 2 additions & 2 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export const getStaticProps: GetStaticProps = async () => {
markets,
uniqueTradingAccounts,
},
revalidate: 60 * 10,
revalidate: 60 * 60,
};
} catch (e) {
console.log(e);
Expand All @@ -259,7 +259,7 @@ export const getStaticProps: GetStaticProps = async () => {
markets: null,
uniqueTradingAccounts: null,
},
revalidate: 5,
revalidate: 60 * 5,
};
}
};
Expand Down
12 changes: 11 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down