homepage/src/utils/proxy/cached-fetch.js

19 lines
409 B
JavaScript
Raw Normal View History

2022-08-24 10:44:35 +03:00
import cache from "memory-cache";
const defaultDuration = 5;
2022-08-24 10:44:35 +03:00
export default async function cachedFetch(url, duration) {
const cached = cache.get(url);
// eslint-disable-next-line no-param-reassign
duration = duration || defaultDuration;
2022-08-24 10:44:35 +03:00
if (cached) {
return cached;
}
2022-09-07 16:53:24 +03:00
const data = await fetch(url).then((res) => res.json());
cache.put(url, data, duration * 1000 * 60);
return data;
2022-08-24 10:44:35 +03:00
}