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:
1 parent
08ca8303f3
commit
d8908c69e9
2 files changed
+31
-9
No files matched your search
+21
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user