-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.js
38 lines (32 loc) · 1.21 KB
/
backend.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
async function fetchDataWithCache(url, cacheKey, cacheDurationInMinutes) {
let cachedData = localStorage.getItem(cacheKey);
let cacheExpiry = localStorage.getItem(cacheKey + '_expiry');
if (cachedData && cacheExpiry && Date.now() < parseInt(cacheExpiry, 10)) {
return JSON.parse(cachedData);
} else {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Failed to fetch data');
}
const responseData = await response.json();
localStorage.setItem(cacheKey, JSON.stringify(responseData));
const expiryTime = Date.now() + cacheDurationInMinutes * 60 * 1000;
localStorage.setItem(cacheKey + '_expiry', expiryTime.toString());
return responseData;
} catch (error) {
console.error('Error fetching data:', error);
throw error;
}
}
}
const cacheKey = 'cached_data';
const cacheDurationInMinutes = 10;
(async () => {
try {
const data = await fetchDataWithCache(apiUrl, cacheKey, cacheDurationInMinutes);
console.log('Data:', data);
} catch (error) {
console.error('Error:', error);
}
})();