Add comprehensive caching for performance optimization

- Cache predicted rating calculations (24 hour duration)
- Cache rating history data to avoid repeated scraping
- Implement separate cache keys for different data types
- Add cache hit logging for monitoring effectiveness
- Update PDGA numbers list with additional players
- Significantly improve performance for repeat visitors
- Reduce load on PDGA servers with intelligent caching

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Samuel Enocsson
2025-08-12 16:34:36 +02:00
parent 2ab4869fb9
commit af54b30c6c
2 changed files with 40 additions and 2 deletions
+38 -1
View File
@@ -511,11 +511,29 @@ function parseRatingHistory(html) {
app.get('/api/rating-history/:pdgaNumber', async (req, res) => {
try {
const { pdgaNumber } = req.params;
console.log(`Fetching rating history for PDGA ${pdgaNumber}...`);
const cacheKey = `history-${pdgaNumber}`;
const cached = cache.get(cacheKey);
// Check cache first (24 hour cache for rating history)
if (cached && Date.now() - cached.timestamp < CACHE_DURATION) {
console.log(`Using cached rating history for PDGA ${pdgaNumber}`);
res.json({
pdgaNumber: parseInt(pdgaNumber),
history: cached.data
});
return;
}
console.log(`Fetching rating history for PDGA ${pdgaNumber}...`);
const html = await fetchRatingHistory(pdgaNumber);
const history = parseRatingHistory(html);
// Cache the result
cache.set(cacheKey, {
data: history,
timestamp: Date.now()
});
res.json({
pdgaNumber: parseInt(pdgaNumber),
history
@@ -530,6 +548,19 @@ app.post('/api/predicted-rating/:pdgaNumber', async (req, res) => {
let browser = null;
try {
const { pdgaNumber } = req.params;
const cacheKey = `predicted-${pdgaNumber}`;
const cached = cache.get(cacheKey);
// Check cache first (24 hour cache for predicted ratings)
if (cached && Date.now() - cached.timestamp < CACHE_DURATION) {
console.log(`Using cached predicted rating for PDGA ${pdgaNumber}`);
res.json({
pdgaNumber: parseInt(pdgaNumber),
predictedRating: cached.data
});
return;
}
browser = await puppeteer.launch({
headless: "new",
args: [
@@ -549,6 +580,12 @@ app.post('/api/predicted-rating/:pdgaNumber', async (req, res) => {
await browser.close();
browser = null;
// Cache the result
cache.set(cacheKey, {
data: predictedRating,
timestamp: Date.now()
});
res.json({
pdgaNumber: parseInt(pdgaNumber),
predictedRating