Cache dagsmart API responses for 1 hour

This commit is contained in:
ulve
2026-03-24 12:08:34 +01:00
parent 7210e234cc
commit a75ccc47a9
+14 -4
View File
@@ -5,13 +5,24 @@ import { dirname, join } from 'path';
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
const cache = new Map();
const TTL = 60 * 60 * 1000; // 1 hour
async function cachedFetch(url) {
const entry = cache.get(url);
if (entry && Date.now() - entry.ts < TTL) return entry.data;
const response = await fetch(url);
const data = await response.json();
cache.set(url, { data, ts: Date.now() });
return data;
}
app.use(express.static(join(__dirname, 'public')));
app.get('/api/holidays', async (req, res) => {
const { year } = req.query;
try {
const response = await fetch(`https://api.dagsmart.se/holidays?year=${year}&weekends=false`);
const data = await response.json();
const data = await cachedFetch(`https://api.dagsmart.se/holidays?year=${year}&weekends=false`);
res.json(data);
} catch (err) {
res.status(500).json({ error: String(err) });
@@ -21,8 +32,7 @@ app.get('/api/holidays', async (req, res) => {
app.get('/api/bridge-days', async (req, res) => {
const { year } = req.query;
try {
const response = await fetch(`https://api.dagsmart.se/bridge-days?year=${year}`);
const data = await response.json();
const data = await cachedFetch(`https://api.dagsmart.se/bridge-days?year=${year}`);
res.json(data);
} catch (err) {
res.status(500).json({ error: String(err) });