Ajout d'un système de cache des
requêtes HTTP
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import {
|
||||
HttpEvent,
|
||||
HttpRequest,
|
||||
HttpResponse,
|
||||
HttpInterceptor,
|
||||
HttpHandler
|
||||
} from "@angular/common/http";
|
||||
|
||||
import { Observable } from "rxjs/Observable";
|
||||
import { startWith, tap } from "rxjs/operators";
|
||||
import "rxjs/add/observable/of";
|
||||
|
||||
import { RequestCache } from "./request-cache.service";
|
||||
|
||||
@Injectable()
|
||||
export class CachingInterceptor implements HttpInterceptor {
|
||||
constructor(private cache: RequestCache) {}
|
||||
|
||||
intercept(req: HttpRequest<any>, next: HttpHandler) {
|
||||
const cachedResponse = this.cache.get(req);
|
||||
return cachedResponse
|
||||
? Observable.of(cachedResponse)
|
||||
: this.sendRequest(req, next);
|
||||
}
|
||||
|
||||
sendRequest(
|
||||
req: HttpRequest<any>,
|
||||
next: HttpHandler
|
||||
): Observable<HttpEvent<any>> {
|
||||
return next.handle(req).pipe(
|
||||
tap(event => {
|
||||
if (event instanceof HttpResponse) {
|
||||
this.cache.put(req, event);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,17 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||
import { Injectable } from "@angular/core";
|
||||
import { HttpClient, HttpHeaders } from "@angular/common/http";
|
||||
|
||||
import { JumpReq } from '../models/jump';
|
||||
import { environment } from '../environments/environment';
|
||||
import { DateService } from './dateService';
|
||||
import { JumpReq } from "../models/jump";
|
||||
import { environment } from "../environments/environment";
|
||||
import { DateService } from "./date.service";
|
||||
|
||||
@Injectable()
|
||||
export class ServiceApiPost {
|
||||
private readonly headers = new HttpHeaders({
|
||||
'Access-Control-Allow-Origin': environment.urlApi
|
||||
"Access-Control-Allow-Origin": environment.urlApi
|
||||
});
|
||||
|
||||
constructor(private http: HttpClient,
|
||||
private dateService: DateService) { }
|
||||
constructor(private http: HttpClient, private dateService: DateService) {}
|
||||
|
||||
public AddListOfJump(
|
||||
selectedJumpType: number,
|
||||
@@ -26,7 +25,8 @@ export class ServiceApiPost {
|
||||
defaultDeployAltitude: number,
|
||||
countOfJumps: number
|
||||
) {
|
||||
const diffInDays = this.dateService.DiffBetweenDates(beginDate, endDate) + 1;
|
||||
const diffInDays =
|
||||
this.dateService.DiffBetweenDates(beginDate, endDate) + 1;
|
||||
const countOfJumpsPerDay = Math.trunc(countOfJumps / diffInDays);
|
||||
|
||||
for (let i = 1; beginDate.getTime() < endDate.getTime(); i++) {
|
||||
@@ -79,15 +79,16 @@ export class ServiceApiPost {
|
||||
exitAltitude: defaultExitAltitude,
|
||||
deployAltitude: defaultDeployAltitude,
|
||||
gearId: selectedRig,
|
||||
notes: '',
|
||||
notes: "",
|
||||
id: 0,
|
||||
jumpDate: jumpDate
|
||||
};
|
||||
|
||||
// console.log(bodyNewjump.jumpDate);
|
||||
this.http.post(`${environment.urlApi}/api/Jump`,
|
||||
bodyNewjump,
|
||||
{ headers: this.headers })
|
||||
this.http
|
||||
.post(`${environment.urlApi}/api/Jump`, bodyNewjump, {
|
||||
headers: this.headers
|
||||
})
|
||||
.subscribe(data => console.log(data));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user