log caches

This commit is contained in:
Dan Percic 2022-07-26 17:42:36 +03:00
parent 2e74e2649e
commit bc799eae4d
2 changed files with 13 additions and 4 deletions

View File

@ -2,7 +2,7 @@ import { Inject, Injectable } from '@angular/core';
import { HttpEvent, HttpHeaders, HttpRequest, HttpResponse } from '@angular/common/http';
import { from, Observable, of, throwError } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';
import { APP_LEVEL_CACHE, DYNAMIC_CACHES } from './cache-utils';
import { APP_LEVEL_CACHE, DYNAMIC_CACHES, wipeCache } from './cache-utils';
import { DynamicCache, DynamicCaches } from './dynamic-cache';
@Injectable()
@ -24,6 +24,7 @@ export class CacheApiService {
for (const dynCache of this._dynamicCaches) {
for (const cacheUrl of dynCache.urls) {
if (url.indexOf(cacheUrl) >= 0) {
console.log('[CACHE-API] open cache: ', dynCache.name);
return caches.open(dynCache.name).then(cache => this._handleFetchResponse(httpResponse, dynCache, cache, url));
}
}
@ -33,15 +34,16 @@ export class CacheApiService {
}
async wipeCaches(logoutDependant = false) {
console.log('[CACHE-API] delete app level cache ');
await caches.delete(APP_LEVEL_CACHE);
for (const cache of this._dynamicCaches) {
if (!logoutDependant) {
await caches.delete(cache.name);
await wipeCache(cache.name);
}
if (logoutDependant && cache.clearOnLogout) {
await caches.delete(cache.name);
await wipeCache(cache.name);
}
}
}
@ -49,6 +51,7 @@ export class CacheApiService {
getCachedRequest(request: HttpRequest<any>): Observable<HttpEvent<any>> {
const url = this._buildUrl(request);
console.log('[CACHE-API] get cached request: ', url);
return from(caches.match(url)).pipe(
mergeMap(response => {
if (response) {
@ -75,6 +78,7 @@ export class CacheApiService {
return Promise.resolve();
}
console.log('[CACHE-API] cache value: ', name);
const cache = await caches.open(APP_LEVEL_CACHE);
const string = JSON.stringify(valueReference);
const expires = new Date().getTime() + ttl * 1000;
@ -93,6 +97,7 @@ export class CacheApiService {
return Promise.resolve(undefined);
}
console.log('[CACHE-API] get cached value: ', name);
const cache = await caches.open(APP_LEVEL_CACHE);
const result = await cache.match(name);
if (!result) {
@ -228,6 +233,7 @@ export class CacheApiService {
}
private async _handleCacheExpiration(dynCache: DynamicCache, now: number) {
console.log('[CACHE-API] checking cache expiration');
const cache = await caches.open(dynCache.name);
let keys = await cache.keys();
// removed expired;

View File

@ -6,17 +6,20 @@ export const APP_LEVEL_CACHE = 'app-level-cache';
export const DYNAMIC_CACHES = new InjectionToken<DynamicCaches>('dynamic-caches');
export async function wipeAllCaches() {
console.log('get caches keys');
const keys = await caches.keys();
for (const cache of keys) {
await caches.delete(cache);
await wipeCache(cache);
}
}
export function wipeCache(cacheName: string) {
console.log('delete cache: ', cacheName);
return caches.delete(cacheName);
}
export async function wipeCacheEntry(cacheName: string, entry: string) {
console.log('open cache: ', cacheName);
const cache = await caches.open(cacheName);
return cache.delete(entry, { ignoreSearch: false });
}