mirror of
https://github.com/itsnaveenk/bazar3.git
synced 2025-12-19 21:17:06 +00:00
Add cache clearing endpoint and improve cache handling for results
This commit is contained in:
parent
1a01bd45d1
commit
08875a26fe
@ -61,4 +61,13 @@ router.get('/results/team', async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.get('/cache/clear', async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const result = await resultService.clearCache();
|
||||||
|
res.json(result);
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
@ -14,7 +14,7 @@ exports.getResultsByTeamAndDate = async (team, date) => {
|
|||||||
const results = await db.query(`
|
const results = await db.query(`
|
||||||
SELECT r.id, r.result_time,
|
SELECT r.id, r.result_time,
|
||||||
CASE
|
CASE
|
||||||
WHEN NOW() < r.result_time THEN '-1'
|
WHEN CONVERT_TZ(NOW(), 'UTC', '+05:30') < r.result_time THEN '-1'
|
||||||
ELSE r.result
|
ELSE r.result
|
||||||
END AS visible_result,
|
END AS visible_result,
|
||||||
t.name AS team
|
t.name AS team
|
||||||
@ -29,7 +29,7 @@ exports.getResultsByTeamAndDate = async (team, date) => {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.set(cacheKey, results);
|
cache.set(cacheKey, results, 60000);
|
||||||
return results;
|
return results;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching results:', error);
|
console.error('Error fetching results:', error);
|
||||||
@ -51,7 +51,7 @@ exports.getTodayResults = async () => {
|
|||||||
const results = await db.query(`
|
const results = await db.query(`
|
||||||
SELECT r.id, t.name AS team, r.result_time,
|
SELECT r.id, t.name AS team, r.result_time,
|
||||||
CASE
|
CASE
|
||||||
WHEN NOW() < r.result_time THEN '-1'
|
WHEN CONVERT_TZ(NOW(), 'UTC', '+05:30') < r.result_time THEN '-1'
|
||||||
ELSE r.result
|
ELSE r.result
|
||||||
END AS visible_result
|
END AS visible_result
|
||||||
FROM results r
|
FROM results r
|
||||||
@ -61,7 +61,7 @@ exports.getTodayResults = async () => {
|
|||||||
`);
|
`);
|
||||||
|
|
||||||
console.log('Caching today\'s results...');
|
console.log('Caching today\'s results...');
|
||||||
cache.set(cacheKey, results);
|
cache.set(cacheKey, results, 60000);
|
||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -82,10 +82,16 @@ exports.getMonthlyResults = async (team, month) => {
|
|||||||
throw { status: 400, message: 'Invalid month format. Use YYYY-MM.' };
|
throw { status: 400, message: 'Invalid month format. Use YYYY-MM.' };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cacheKey = `monthly:${team}:${month}`;
|
||||||
|
if (cache.has(cacheKey)) {
|
||||||
|
console.log('Cache hit for monthly results.');
|
||||||
|
return cache.get(cacheKey);
|
||||||
|
}
|
||||||
|
|
||||||
const results = await db.query(`
|
const results = await db.query(`
|
||||||
SELECT r.result_time,
|
SELECT r.result_time,
|
||||||
CASE
|
CASE
|
||||||
WHEN NOW() < r.result_time THEN '-1'
|
WHEN CONVERT_TZ(NOW(), 'UTC', '+05:30') < r.result_time THEN '-1'
|
||||||
ELSE r.result
|
ELSE r.result
|
||||||
END AS visible_result
|
END AS visible_result
|
||||||
FROM results r
|
FROM results r
|
||||||
@ -94,6 +100,7 @@ exports.getMonthlyResults = async (team, month) => {
|
|||||||
ORDER BY r.result_time DESC
|
ORDER BY r.result_time DESC
|
||||||
`, [team.toUpperCase(), month]);
|
`, [team.toUpperCase(), month]);
|
||||||
|
|
||||||
|
cache.set(cacheKey, results, 300000); // Cache for 5 minutes
|
||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -102,18 +109,26 @@ exports.getDailyResults = async (date) => {
|
|||||||
throw { status: 400, message: 'Date is required.' };
|
throw { status: 400, message: 'Date is required.' };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cacheKey = `daily:${date}`;
|
||||||
|
if (cache.has(cacheKey)) {
|
||||||
|
console.log('Cache hit for daily results.');
|
||||||
|
return cache.get(cacheKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use CONVERT_TZ to explicitly convert timestamps to IST
|
||||||
const results = await db.query(`
|
const results = await db.query(`
|
||||||
SELECT r.id, t.name AS team, r.result_time,
|
SELECT r.id, t.name AS team, r.result_time,
|
||||||
CASE
|
CASE
|
||||||
WHEN NOW() < r.result_time THEN '-1'
|
WHEN CONVERT_TZ(NOW(), 'UTC', '+05:30') < r.result_time THEN '-1'
|
||||||
ELSE r.result
|
ELSE r.result
|
||||||
END AS visible_result
|
END AS visible_result
|
||||||
FROM results r
|
FROM results r
|
||||||
JOIN teams t ON r.team_id = t.id
|
JOIN teams t ON r.team_id = t.id
|
||||||
WHERE DATE(r.result_time) = ?
|
WHERE DATE(CONVERT_TZ(r.result_time, 'UTC', '+05:30')) = ?
|
||||||
ORDER BY r.result_time DESC
|
ORDER BY r.result_time DESC
|
||||||
`, [date]);
|
`, [date]);
|
||||||
|
|
||||||
|
cache.set(cacheKey, results, 60000);
|
||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -130,7 +145,7 @@ exports.getResultsByTeam = async (team) => {
|
|||||||
const results = await db.query(`
|
const results = await db.query(`
|
||||||
SELECT r.result_time,
|
SELECT r.result_time,
|
||||||
CASE
|
CASE
|
||||||
WHEN NOW() < r.result_time THEN '-1'
|
WHEN CONVERT_TZ(NOW(), 'UTC', '+05:30') < r.result_time THEN '-1'
|
||||||
ELSE r.result
|
ELSE r.result
|
||||||
END AS visible_result,
|
END AS visible_result,
|
||||||
t.name AS team
|
t.name AS team
|
||||||
@ -140,6 +155,12 @@ exports.getResultsByTeam = async (team) => {
|
|||||||
ORDER BY r.result_time DESC
|
ORDER BY r.result_time DESC
|
||||||
`, [team.toUpperCase()]);
|
`, [team.toUpperCase()]);
|
||||||
|
|
||||||
cache.set(cacheKey, results);
|
cache.set(cacheKey, results, 60000);
|
||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.clearCache = async () => {
|
||||||
|
console.log('Manually clearing all cache...');
|
||||||
|
cache.clear();
|
||||||
|
return { success: true, message: 'Cache cleared successfully', timestamp: new Date().toISOString() };
|
||||||
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user