Interceptor cache sudut
import {
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest,
HttpResponse,
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, of, tap } from 'rxjs';
@Injectable()
export class CachingInterceptor implements HttpInterceptor {
private cache = new Map<string, any>();
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
if (request.method !== 'GET') {
return next.handle(request);
}
const cachedResponse = this.cache.get(request.url);
if (cachedResponse) {
return of(cachedResponse);
}
return next.handle(request).pipe(
tap((response) => {
if (response instanceof HttpResponse) {
this.cache.set(request.url, response);
}
})
);
}
}
Touqeer Hassan