Improve the SVG fetching and caching logic (#2011)
Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {SafeHtml} from '@angular/platform-browser';
|
||||
import {Observable, BehaviorSubject} from 'rxjs';
|
||||
import {BehaviorSubject} from 'rxjs';
|
||||
|
||||
interface CachedIcon {
|
||||
content: string;
|
||||
sanitized: SafeHtml;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
@@ -13,25 +12,9 @@ interface CachedIcon {
|
||||
})
|
||||
export class IconCacheService {
|
||||
private cache = new Map<string, CachedIcon>();
|
||||
private readonly CACHE_DURATION_MS = 1000 * 60 * 60;
|
||||
|
||||
private cacheUpdate$ = new BehaviorSubject<string | null>(null);
|
||||
|
||||
getCachedContent(iconName: string): string | null {
|
||||
const cached = this.cache.get(iconName);
|
||||
|
||||
if (!cached) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Date.now() - cached.timestamp > this.CACHE_DURATION_MS) {
|
||||
this.cache.delete(iconName);
|
||||
return null;
|
||||
}
|
||||
|
||||
return cached.content;
|
||||
}
|
||||
|
||||
getCachedSanitized(iconName: string): SafeHtml | null {
|
||||
const cached = this.cache.get(iconName);
|
||||
|
||||
@@ -39,55 +22,22 @@ export class IconCacheService {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Date.now() - cached.timestamp > this.CACHE_DURATION_MS) {
|
||||
this.cache.delete(iconName);
|
||||
return null;
|
||||
}
|
||||
|
||||
return cached.sanitized;
|
||||
}
|
||||
|
||||
cacheIcon(iconName: string, content: string, sanitized: SafeHtml): void {
|
||||
this.cache.set(iconName, {
|
||||
content,
|
||||
sanitized,
|
||||
timestamp: Date.now()
|
||||
sanitized
|
||||
});
|
||||
this.cacheUpdate$.next(iconName);
|
||||
}
|
||||
|
||||
isCached(iconName: string): boolean {
|
||||
const cached = this.cache.get(iconName);
|
||||
|
||||
if (!cached) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Date.now() - cached.timestamp > this.CACHE_DURATION_MS) {
|
||||
this.cache.delete(iconName);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
removeIcon(iconName: string): boolean {
|
||||
return this.cache.delete(iconName);
|
||||
}
|
||||
|
||||
invalidate(iconName: string): void {
|
||||
this.cache.delete(iconName);
|
||||
this.cacheUpdate$.next(null);
|
||||
}
|
||||
|
||||
invalidateMultiple(iconNames: string[]): void {
|
||||
iconNames.forEach(name => this.cache.delete(name));
|
||||
this.cacheUpdate$.next(null);
|
||||
}
|
||||
|
||||
clearCache(): void {
|
||||
this.cache.clear();
|
||||
this.cacheUpdate$.next(null);
|
||||
}
|
||||
|
||||
getCacheUpdates(): Observable<string | null> {
|
||||
return this.cacheUpdate$.asObservable();
|
||||
getAllIconNames(): string[] {
|
||||
return Array.from(this.cache.keys()).sort();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,11 @@
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {Observable, of} from 'rxjs';
|
||||
import {tap} from 'rxjs/operators';
|
||||
import {finalize, map, shareReplay, tap} from 'rxjs/operators';
|
||||
import {API_CONFIG} from '../../core/config/api-config';
|
||||
import {IconCacheService} from './icon-cache.service';
|
||||
import {DomSanitizer, SafeHtml} from '@angular/platform-browser';
|
||||
|
||||
interface PageResponse<T> {
|
||||
content: T[];
|
||||
number: number;
|
||||
size: number;
|
||||
totalElements: number;
|
||||
totalPages: number;
|
||||
}
|
||||
|
||||
interface SvgIconData {
|
||||
svgName: string;
|
||||
svgData: string;
|
||||
@@ -32,44 +24,65 @@ interface SvgIconBatchResponse {
|
||||
results: IconSaveResult[];
|
||||
}
|
||||
|
||||
interface IconContentMap {
|
||||
[iconName: string]: string;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class IconService {
|
||||
|
||||
private readonly baseUrl = `${API_CONFIG.BASE_URL}/api/v1/icons`;
|
||||
private requestCache = new Map<string, Observable<string>>();
|
||||
private preloadCache$: Observable<void> | null = null;
|
||||
|
||||
private http = inject(HttpClient);
|
||||
private iconCache = inject(IconCacheService);
|
||||
private sanitizer = inject(DomSanitizer);
|
||||
|
||||
saveSvgIcon(svgContent: string, svgName: string): Observable<any> {
|
||||
return this.http.post(this.baseUrl, {
|
||||
svgData: svgContent,
|
||||
svgName: svgName
|
||||
});
|
||||
}
|
||||
preloadAllIcons(): Observable<void> {
|
||||
if (this.preloadCache$) {
|
||||
return this.preloadCache$;
|
||||
}
|
||||
|
||||
getIconNames(page: number = 0, size: number = 50): Observable<PageResponse<string>> {
|
||||
return this.http.get<PageResponse<string>>(this.baseUrl, {
|
||||
params: {page: page.toString(), size: size.toString()}
|
||||
});
|
||||
this.preloadCache$ = this.http.get<IconContentMap>(`${this.baseUrl}/all/content`).pipe(
|
||||
tap((iconsMap) => {
|
||||
Object.entries(iconsMap).forEach(([iconName, content]) => {
|
||||
const sanitized = this.sanitizer.bypassSecurityTrustHtml(content);
|
||||
this.iconCache.cacheIcon(iconName, content, sanitized);
|
||||
});
|
||||
}),
|
||||
map(() => void 0),
|
||||
shareReplay({bufferSize: 1, refCount: false}),
|
||||
finalize(() => this.preloadCache$ = null)
|
||||
);
|
||||
|
||||
return this.preloadCache$;
|
||||
}
|
||||
|
||||
getSvgIconContent(iconName: string): Observable<string> {
|
||||
const cached = this.iconCache.getCachedContent(iconName);
|
||||
const cached = this.iconCache.getCachedSanitized(iconName);
|
||||
if (cached) {
|
||||
return of(cached);
|
||||
return of('');
|
||||
}
|
||||
|
||||
return this.http.get(`${this.baseUrl}/${encodeURIComponent(iconName)}/content`, {
|
||||
responseType: 'text'
|
||||
}).pipe(
|
||||
tap(content => {
|
||||
const sanitized = this.sanitizer.bypassSecurityTrustHtml(content);
|
||||
this.iconCache.cacheIcon(iconName, content, sanitized);
|
||||
})
|
||||
);
|
||||
if (!this.requestCache.has(iconName)) {
|
||||
const request$ = this.http.get(`${this.baseUrl}/${encodeURIComponent(iconName)}/content`, {
|
||||
responseType: 'text'
|
||||
}).pipe(
|
||||
tap(content => {
|
||||
const sanitized = this.sanitizer.bypassSecurityTrustHtml(content);
|
||||
this.iconCache.cacheIcon(iconName, content, sanitized);
|
||||
}),
|
||||
shareReplay({bufferSize: 1, refCount: true}),
|
||||
finalize(() => this.requestCache.delete(iconName))
|
||||
);
|
||||
|
||||
this.requestCache.set(iconName, request$);
|
||||
}
|
||||
|
||||
return this.requestCache.get(iconName)!;
|
||||
}
|
||||
|
||||
getSanitizedSvgContent(iconName: string): Observable<SafeHtml> {
|
||||
@@ -95,7 +108,8 @@ export class IconService {
|
||||
deleteSvgIcon(svgName: string): Observable<any> {
|
||||
return this.http.delete(`${this.baseUrl}/${encodeURIComponent(svgName)}`).pipe(
|
||||
tap(() => {
|
||||
this.iconCache.invalidate(svgName);
|
||||
this.iconCache.removeIcon(svgName);
|
||||
this.requestCache.delete(svgName);
|
||||
})
|
||||
);
|
||||
}
|
||||
@@ -103,11 +117,15 @@ export class IconService {
|
||||
saveBatchSvgIcons(icons: SvgIconData[]): Observable<SvgIconBatchResponse> {
|
||||
return this.http.post<SvgIconBatchResponse>(`${this.baseUrl}/batch`, {icons}).pipe(
|
||||
tap((response) => {
|
||||
const successfulIcons = response.results
|
||||
.filter(result => result.success)
|
||||
.map(result => result.iconName);
|
||||
|
||||
this.iconCache.invalidateMultiple(successfulIcons);
|
||||
response.results.forEach(result => {
|
||||
if (result.success) {
|
||||
const iconData = icons.find(icon => icon.svgName === result.iconName);
|
||||
if (iconData) {
|
||||
const sanitized = this.sanitizer.bypassSecurityTrustHtml(iconData.svgData);
|
||||
this.iconCache.cacheIcon(iconData.svgName, iconData.svgData, sanitized);
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user