optimising and making it better

This commit is contained in:
sdarbinyan
2026-02-26 21:54:21 +04:00
parent 7a00a8f1e3
commit 10b4974719
58 changed files with 318 additions and 1804 deletions

View File

@@ -2,7 +2,7 @@ import { HttpInterceptorFn, HttpResponse } from '@angular/common/http';
import { of } from 'rxjs';
import { tap } from 'rxjs/operators';
const cache = new Map<string, { response: HttpResponse<any>, timestamp: number }>();
const cache = new Map<string, { response: HttpResponse<unknown>, timestamp: number }>();
const CACHE_DURATION = 5 * 60 * 1000; // 5 минут
export const cacheInterceptor: HttpInterceptorFn = (req, next) => {
@@ -17,6 +17,9 @@ export const cacheInterceptor: HttpInterceptorFn = (req, next) => {
return next(req);
}
// Cleanup expired entries before checking
cleanupExpiredCache();
const cachedResponse = cache.get(req.url);
// Проверяем наличие и актуальность кэша
@@ -25,7 +28,6 @@ export const cacheInterceptor: HttpInterceptorFn = (req, next) => {
if (age < CACHE_DURATION) {
return of(cachedResponse.response.clone());
} else {
// Кэш устарел, удаляем
cache.delete(req.url);
}
}
@@ -43,19 +45,16 @@ export const cacheInterceptor: HttpInterceptorFn = (req, next) => {
);
};
// Функция для очистки кэша (можно использовать при необходимости)
/** Clear all cached responses */
export function clearCache(): void {
cache.clear();
// console.log('[Cache] Cache cleared');
}
// Функция для очистки устаревшего кэша
export function cleanupExpiredCache(): void {
function cleanupExpiredCache(): void {
const now = Date.now();
for (const [url, data] of cache.entries()) {
if (now - data.timestamp >= CACHE_DURATION) {
cache.delete(url);
}
}
// console.log('[Cache] Expired cache cleaned up');
}