From af54b30c6c083db24a4e4a5f618eff46d6b0788e Mon Sep 17 00:00:00 2001 From: Samuel Enocsson Date: Tue, 12 Aug 2025 16:34:36 +0200 Subject: [PATCH] Add comprehensive caching for performance optimization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- pdga-numbers.txt | 3 ++- server.js | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/pdga-numbers.txt b/pdga-numbers.txt index 2eaed08..567898b 100644 --- a/pdga-numbers.txt +++ b/pdga-numbers.txt @@ -5,4 +5,5 @@ 176006 201220 288059 -242229 \ No newline at end of file +242229 +155816 \ No newline at end of file diff --git a/server.js b/server.js index 3813a12..2eee101 100644 --- a/server.js +++ b/server.js @@ -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