Ajout d'un système de cache des
requêtes HTTP
This commit is contained in:
35
Front/skydivelogs-app/src/services/request-cache.service.ts
Normal file
35
Front/skydivelogs-app/src/services/request-cache.service.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { HttpRequest, HttpResponse } from "@angular/common/http";
|
||||
|
||||
const maxAge = 30000;
|
||||
@Injectable()
|
||||
export class RequestCache {
|
||||
cache = new Map();
|
||||
|
||||
get(req: HttpRequest<any>): HttpResponse<any> | undefined {
|
||||
const url = req.urlWithParams + "-" + req.method;
|
||||
const cached = this.cache.get(url);
|
||||
|
||||
if (!cached) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return cached.response;
|
||||
}
|
||||
|
||||
put(req: HttpRequest<any>, response: HttpResponse<any>): void {
|
||||
//const url = req.url;
|
||||
const url = req.urlWithParams + "-" + req.method;
|
||||
const entry = { url, response, lastRead: Date.now() };
|
||||
this.cache.set(url, entry);
|
||||
|
||||
console.log(this.cache);
|
||||
|
||||
const expired = Date.now() - maxAge;
|
||||
this.cache.forEach(expiredEntry => {
|
||||
if (expiredEntry.lastRead < expired) {
|
||||
this.cache.delete(expiredEntry.url);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user