-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcacheApiCall.js
59 lines (50 loc) · 1.58 KB
/
cacheApiCall.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const cacheApiCall = (time) => {
const cache = {};
return async (url, config = {}) => {
const key = `${url}-${JSON.stringify(config)}`;
const entry = cache[key];
// Check if entry exists and is not expired
if (!entry || Date.now() > entry.expiry) {
try {
console.log("Making new API call");
const response = await fetch(url, config).then((res) => res.json());
// Cache the response with an expiration time
cache[key] = { value: response, expiry: Date.now() + time };
// Set a timeout to delete the cache after it expires
setTimeout(() => {
if (Date.now() > cache[key]?.expiry) {
delete cache[key];
console.log(`Cache expired and removed for key: ${key}`);
}
}, time);
} catch (err) {
console.error("API call failed:", err);
}
}
// Return the cached value
return cache[key]?.value;
};
};
// Usage example
const call = cacheApiCall(1500);
call("https://jsonplaceholder.typicode.com/todos/1").then((data) =>
console.log(data)
);
setTimeout(() => {
// Returns data from cache if within cache time
call("https://jsonplaceholder.typicode.com/todos/1").then((data) =>
console.log(data)
);
}, 1000);
setTimeout(() => {
// Makes a new API call after cache expires
call("https://jsonplaceholder.typicode.com/todos/1").then((data) =>
console.log(data)
);
}, 4000);
setTimeout(() => {
// Returns data from cache if within cache time
call("https://jsonplaceholder.typicode.com/todos/1").then((data) =>
console.log(data)
);
}, 5000);