Petits fix sur le cache des requêtes Http
This commit is contained in:
@@ -1,21 +1,21 @@
|
|||||||
import { Injectable } from "@angular/core";
|
import { Injectable } from '@angular/core';
|
||||||
import {
|
import {
|
||||||
HttpEvent,
|
HttpEvent,
|
||||||
HttpRequest,
|
HttpRequest,
|
||||||
HttpResponse,
|
HttpResponse,
|
||||||
HttpInterceptor,
|
HttpInterceptor,
|
||||||
HttpHandler
|
HttpHandler
|
||||||
} from "@angular/common/http";
|
} from '@angular/common/http';
|
||||||
|
|
||||||
import { Observable } from "rxjs/Observable";
|
import { Observable } from 'rxjs/Observable';
|
||||||
import { startWith, tap } from "rxjs/operators";
|
import { startWith, tap } from 'rxjs/operators';
|
||||||
import "rxjs/add/observable/of";
|
import 'rxjs/add/observable/of';
|
||||||
|
|
||||||
import { RequestCache } from "./request-cache.service";
|
import { RequestCache } from './request-cache.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CachingInterceptor implements HttpInterceptor {
|
export class CachingInterceptor implements HttpInterceptor {
|
||||||
constructor(private cache: RequestCache) {}
|
constructor(private cache: RequestCache) { }
|
||||||
|
|
||||||
intercept(req: HttpRequest<any>, next: HttpHandler) {
|
intercept(req: HttpRequest<any>, next: HttpHandler) {
|
||||||
const cachedResponse = this.cache.get(req);
|
const cachedResponse = this.cache.get(req);
|
||||||
|
|||||||
@@ -1,31 +1,41 @@
|
|||||||
import { Injectable } from "@angular/core";
|
import { Injectable } from '@angular/core';
|
||||||
import { HttpRequest, HttpResponse } from "@angular/common/http";
|
import { HttpRequest, HttpResponse } from '@angular/common/http';
|
||||||
|
|
||||||
const maxAge = 30000;
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class RequestCache {
|
export class RequestCache {
|
||||||
cache = new Map();
|
private cache: Map<string, any>;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.cache = new Map<string, any>();
|
||||||
|
}
|
||||||
|
|
||||||
get(req: HttpRequest<any>): HttpResponse<any> | undefined {
|
get(req: HttpRequest<any>): HttpResponse<any> | undefined {
|
||||||
const url = req.urlWithParams + "-" + req.method;
|
const splittedUrl = req.urlWithParams.split('/');
|
||||||
|
const url = splittedUrl[splittedUrl.length - 1] + '_' + req.method;
|
||||||
|
this.clearExpiredEntries();
|
||||||
const cached = this.cache.get(url);
|
const cached = this.cache.get(url);
|
||||||
|
|
||||||
if (!cached) {
|
let cachedResponse: HttpResponse<any>;
|
||||||
return undefined;
|
if (cached) {
|
||||||
|
cachedResponse = cached.response;
|
||||||
}
|
}
|
||||||
|
|
||||||
return cached.response;
|
return cachedResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
put(req: HttpRequest<any>, response: HttpResponse<any>): void {
|
put(req: HttpRequest<any>, response: HttpResponse<any>): void {
|
||||||
//const url = req.url;
|
const splittedUrl = req.urlWithParams.split('/');
|
||||||
const url = req.urlWithParams + "-" + req.method;
|
const url = splittedUrl[splittedUrl.length - 1] + '_' + req.method;
|
||||||
|
|
||||||
const entry = { url, response, lastRead: Date.now() };
|
const entry = { url, response, lastRead: Date.now() };
|
||||||
this.cache.set(url, entry);
|
this.cache.set(url, entry);
|
||||||
|
this.clearExpiredEntries();
|
||||||
|
}
|
||||||
|
|
||||||
console.log(this.cache);
|
private clearExpiredEntries(): void {
|
||||||
|
const maxAge = 30000;
|
||||||
const expired = Date.now() - maxAge;
|
const expired = Date.now() - maxAge;
|
||||||
|
|
||||||
this.cache.forEach(expiredEntry => {
|
this.cache.forEach(expiredEntry => {
|
||||||
if (expiredEntry.lastRead < expired) {
|
if (expiredEntry.lastRead < expired) {
|
||||||
this.cache.delete(expiredEntry.url);
|
this.cache.delete(expiredEntry.url);
|
||||||
|
|||||||
Reference in New Issue
Block a user