Add cache management and smart delay optimization

- Add subtle "clear cache" link (gear icon) in top-right corner
- Implement cache clearing endpoint with user feedback
- Fix delay logic to skip delays for cached data
- Only apply rate limiting delays when actually scraping fresh data
- Add cache size reporting when clearing cache
- Improve performance for repeat visits with cached data
- Maintain server-friendly rate limiting for fresh scrapes

🤖 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 17:04:14 +02:00
parent 55188a8269
commit 2994f221f7
2 changed files with 50 additions and 2 deletions
+24 -2
View File
@@ -371,6 +371,11 @@ async function getAllRatings(progressCallback = null) {
}
try {
// Check if data is cached BEFORE scraping
const cacheKey = `player-${pdgaNumber}`;
const cached = cache.get(cacheKey);
const wasFromCache = cached && Date.now() - cached.timestamp < CACHE_DURATION;
const playerData = await scrapePDGARating(pdgaNumber);
ratings.push(playerData);
@@ -384,8 +389,10 @@ async function getAllRatings(progressCallback = null) {
});
}
// Longer delay to avoid overwhelming the server
await new Promise(resolve => setTimeout(resolve, 1000));
if (!wasFromCache) {
// Delay only for fresh scrapes to avoid overwhelming the server
await new Promise(resolve => setTimeout(resolve, 1000));
}
} catch (error) {
console.error(`Failed to scrape PDGA ${pdgaNumber}:`, error.message);
const errorData = {
@@ -576,6 +583,21 @@ app.get('/api/rating-history/:pdgaNumber', async (req, res) => {
}
});
app.post('/api/clear-cache', (req, res) => {
try {
const cacheSize = cache.size;
cache.clear();
console.log(`Cache cleared - removed ${cacheSize} entries`);
res.json({
success: true,
message: `Cache cleared - ${cacheSize} entries removed`
});
} catch (error) {
console.error('Error clearing cache:', error);
res.status(500).json({ error: 'Failed to clear cache' });
}
});
app.post('/api/predicted-rating/:pdgaNumber', async (req, res) => {
let browser = null;
try {