red-ui/libs/iqser-cache/src/lib/caches/cache-utils.ts

50 lines
1.2 KiB
TypeScript

export const APP_LEVEL_CACHE = 'app-level-cache';
export const DYNAMIC_CACHES = [
{
urls: ['/assets/icons', '/assets/wv-resources'],
name: 'cached-assets',
clearOnLogout: false,
maxAge: 24 * 3600,
maxSize: 1000,
methods: ['GET'],
},
{
urls: ['/download/original'],
name: 'files',
maxAge: 3600 * 24 * 7,
maxSize: 1000,
clearOnLogout: true,
methods: ['GET'],
},
];
export async function wipeAllCaches() {
const keys = await caches.keys();
for (const cache of keys) {
await caches.delete(cache);
}
}
export async function wipeCaches(logoutDependant: boolean = false) {
await caches.delete(APP_LEVEL_CACHE);
for (const cache of DYNAMIC_CACHES) {
if (!logoutDependant) {
await caches.delete(cache.name);
}
if (logoutDependant && cache.clearOnLogout) {
await caches.delete(cache.name);
}
}
}
export function wipeFilesCache() {
return caches.delete('files');
}
export async function wipeCacheEntry(cacheName: string, entry: string) {
return caches.open(cacheName).then(cache => {
cache.delete(entry, { ignoreSearch: false });
});
}