Skip to content

Commit c073b3f

Browse files
feat(cache): permitir uso de dados expirados como fallback em chamadas de API
1 parent 0c7a2f1 commit c073b3f

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

docs/index.html

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,14 +2551,18 @@ <h4>Comunidade</h4>
25512551
CACHE_KEY: 'wwm_api_cache_index',
25522552
CACHE_TTL: 10 * 60 * 1000, // 10 minutos
25532553

2554-
get(key) {
2554+
get(key, allowExpired = false) {
25552555
try {
25562556
const cache = JSON.parse(localStorage.getItem(this.CACHE_KEY) || '{}');
25572557
const item = cache[key];
25582558
if (item && Date.now() - item.timestamp < this.CACHE_TTL) {
25592559
console.log(`[Cache HIT] ${key}`);
25602560
return item.data;
25612561
}
2562+
if (item && allowExpired) {
2563+
console.log(`[Cache STALE] ${key} (usando dados antigos)`);
2564+
return item.data;
2565+
}
25622566
if (item) {
25632567
console.log(`[Cache EXPIRED] ${key}`);
25642568
}
@@ -2584,19 +2588,43 @@ <h4>Comunidade</h4>
25842588
}
25852589
};
25862590

2587-
// API call com cache
2591+
// API call com cache e fallback para dados expirados
25882592
async function cachedApiCall(cacheKey, url, options = {}) {
2593+
// Primeiro tenta cache válido
25892594
const cached = CacheManager.get(cacheKey);
25902595
if (cached) return cached;
25912596

2592-
const response = await fetch(url, options);
2593-
if (!response.ok) {
2594-
throw new Error(`API error: ${response.status}`);
2597+
try {
2598+
const response = await fetch(url, options);
2599+
2600+
// Rate limit atingido
2601+
if (response.status === 403) {
2602+
const staleData = CacheManager.get(cacheKey, true); // allowExpired
2603+
if (staleData) {
2604+
console.warn(`[Rate Limit] Usando cache expirado para ${cacheKey}`);
2605+
return staleData;
2606+
}
2607+
const resetTime = response.headers.get('X-RateLimit-Reset');
2608+
const resetDate = resetTime ? new Date(parseInt(resetTime) * 1000).toLocaleTimeString('pt-BR') : 'em breve';
2609+
throw new Error(`Rate limit excedido. Tente novamente às ${resetDate}`);
2610+
}
2611+
2612+
if (!response.ok) {
2613+
throw new Error(`API error: ${response.status}`);
2614+
}
2615+
2616+
const data = await response.json();
2617+
CacheManager.set(cacheKey, data);
2618+
return data;
2619+
} catch (error) {
2620+
// Em caso de erro de rede, tenta cache expirado
2621+
const staleData = CacheManager.get(cacheKey, true);
2622+
if (staleData) {
2623+
console.warn(`[Fallback] Usando cache expirado para ${cacheKey}`);
2624+
return staleData;
2625+
}
2626+
throw error;
25952627
}
2596-
2597-
const data = await response.json();
2598-
CacheManager.set(cacheKey, data);
2599-
return data;
26002628
}
26012629

26022630
// ========== STATE ==========

docs/translate.html

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,7 +2431,7 @@ <h4><i class="fas fa-clipboard-list"></i> Sugestões (<span id="cart-count">0</s
24312431
},
24322432

24332433
// Recupera dados do cache se ainda válidos
2434-
get(key) {
2434+
get(key, allowExpired = false) {
24352435
try {
24362436
const cached = localStorage.getItem(CONFIG.CACHE_PREFIX + key);
24372437
if (!cached) return null;
@@ -2444,8 +2444,12 @@ <h4><i class="fas fa-clipboard-list"></i> Sugestões (<span id="cart-count">0</s
24442444
return cacheItem.data;
24452445
}
24462446

2447+
if (allowExpired) {
2448+
console.log(`[Cache STALE] ${key} (usando dados antigos)`);
2449+
return cacheItem.data;
2450+
}
2451+
24472452
console.log(`[Cache EXPIRED] ${key}`);
2448-
localStorage.removeItem(CONFIG.CACHE_PREFIX + key);
24492453
return null;
24502454
} catch (e) {
24512455
return null;
@@ -2517,6 +2521,12 @@ <h4><i class="fas fa-clipboard-list"></i> Sugestões (<span id="cart-count">0</s
25172521
const resetDate = new Date(parseInt(resetTime) * 1000);
25182522
console.error(`[API] Rate limit exceeded. Resets at: ${resetDate.toLocaleTimeString()}`);
25192523
}
2524+
// Tenta usar dados expirados como fallback
2525+
const staleData = apiCache.get(cacheKey, true);
2526+
if (staleData) {
2527+
console.warn(`[API] Usando cache expirado como fallback para ${cacheKey}`);
2528+
return { ok: true, data: staleData, fromCache: true, stale: true };
2529+
}
25202530
}
25212531
return { ok: false, status: response.status, data: null };
25222532
}
@@ -2529,6 +2539,12 @@ <h4><i class="fas fa-clipboard-list"></i> Sugestões (<span id="cart-count">0</s
25292539
return { ok: true, data: data, fromCache: false };
25302540
} catch (error) {
25312541
console.error(`[API] Fetch error for ${cacheKey}:`, error);
2542+
// Em caso de erro de rede, tenta cache expirado
2543+
const staleData = apiCache.get(cacheKey, true);
2544+
if (staleData) {
2545+
console.warn(`[API] Usando cache expirado como fallback para ${cacheKey}`);
2546+
return { ok: true, data: staleData, fromCache: true, stale: true };
2547+
}
25322548
return { ok: false, data: null, error };
25332549
}
25342550
}

0 commit comments

Comments
 (0)