feat: add session-level API cache — skip spinners on home/albums/artists/playlists revisit

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
HoneyandClaude Sonnet 4.6 committed 2026-06-06 11:45:22 +02:00
1 parent 08ca8303f3
commit d8908c69e9
2 files changed
+31 -9

No files matched your search

+21
View File
@@ -15,6 +15,27 @@ export async function api(method, extra='') {
if(r.status !== 'ok') throw new Error(r.error?.message || 'API Error');
return r;
}
// ─── API CACHE ───
const _apiCache = new Map(); // key → { data, ts }
const _CACHE_TTL = 5 * 60 * 1000; // 5 min default
export function apiIsCached(method, extra = '') {
const hit = _apiCache.get(method + extra);
return !!(hit && Date.now() - hit.ts < _CACHE_TTL);
}
export async function apiCached(method, extra = '', ttl = _CACHE_TTL) {
const key = method + extra;
const hit = _apiCache.get(key);
if (hit && Date.now() - hit.ts < ttl) return hit.data;
const data = await api(method, extra);
_apiCache.set(key, { data, ts: Date.now() });
return data;
}
export function invalidateApiCache(method, extra) {
if (method) _apiCache.delete(method + (extra || ''));
else _apiCache.clear();
}
export function streamUrl(id) { return apiUrl('stream', `&id=${id}&format=raw`); }
// Stable per-session cache so the same id+size always produces the same URL.