feat: เพิ่มระบบ caching
- เพิ่ม caching interceptor และ service - เพิ่ม configสำหรับ caching
This commit is contained in:
41
accounting-ng-nuttakit/src/app/services/caching.service.ts
Normal file
41
accounting-ng-nuttakit/src/app/services/caching.service.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpResponse } from '@angular/common/http';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class CachingService {
|
||||
private cache = new Map<string, [Date, HttpResponse<any>]>();
|
||||
private cacheDurationInMs = 600000; // 5 minutes
|
||||
|
||||
constructor() { }
|
||||
|
||||
get(key: string): HttpResponse<any> | null {
|
||||
const tuple = this.cache.get(key);
|
||||
if (!tuple) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const expires = tuple[0];
|
||||
const httpResponse = tuple[1];
|
||||
|
||||
// Don't observe expired keys
|
||||
const now = new Date();
|
||||
if (expires && expires.getTime() < now.getTime()) {
|
||||
this.cache.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
return httpResponse;
|
||||
}
|
||||
|
||||
put(key: string, value: HttpResponse<any>): void {
|
||||
const expires = new Date();
|
||||
expires.setMilliseconds(expires.getMilliseconds() + this.cacheDurationInMs);
|
||||
this.cache.set(key, [expires, value]);
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.cache.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user