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:
+26
@@ -172,6 +172,9 @@
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>PDGA Player Ratings</h1>
|
||||
<div style="position: absolute; top: 10px; right: 15px;">
|
||||
<a href="#" onclick="clearCache(); return false;" style="color: #ccc; font-size: 10px; text-decoration: none; opacity: 0.3;" title="Clear cache">⚙</a>
|
||||
</div>
|
||||
<div id="loading" class="loading" style="display: none;">Loading ratings...</div>
|
||||
<div id="progress-section" style="display: none;">
|
||||
<div class="progress-container">
|
||||
@@ -512,6 +515,29 @@
|
||||
}, 100);
|
||||
}
|
||||
|
||||
async function clearCache() {
|
||||
try {
|
||||
const response = await fetch('/api/clear-cache', {
|
||||
method: 'POST'
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
alert(data.message);
|
||||
// Optionally reload the page to reflect fresh data
|
||||
if (confirm('Reload page to fetch fresh data?')) {
|
||||
location.reload();
|
||||
}
|
||||
} else {
|
||||
alert('Failed to clear cache');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error clearing cache:', error);
|
||||
alert('Error clearing cache');
|
||||
}
|
||||
}
|
||||
|
||||
fetchRatingsWithProgress();
|
||||
</script>
|
||||
</body>
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user