mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-28 14:58:47 +00:00
feat: Add client-side caching layer to web dashboard with configurable TTL and pattern-based invalidation
- Add CACHE_TTL environment variable with 30000ms default to API configuration - Add DataCache class with Map-based storage, timestamp tracking, and TTL expiration - Add cache methods for set, get, invalidate, invalidatePattern, clear, and size operations - Add getCachedData helper function with fetcher callback and TTL override support - Add apiFetch wrapper with optional cache parameter for GET
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
const API_BASE = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000";
|
||||
const CACHE_TTL = parseInt(process.env.NEXT_PUBLIC_CACHE_TTL || "30000");
|
||||
|
||||
let authToken: string | null = null;
|
||||
|
||||
import { getCachedData, invalidateCachePattern } from "./cache";
|
||||
|
||||
export function setAuthToken(token: string) {
|
||||
authToken = token;
|
||||
if (typeof window !== "undefined") {
|
||||
@@ -22,9 +25,22 @@ export function clearAuthToken() {
|
||||
if (typeof window !== "undefined") {
|
||||
localStorage.removeItem("auth_token");
|
||||
}
|
||||
invalidateCachePattern(".*");
|
||||
}
|
||||
|
||||
async function apiFetch<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
async function apiFetch<T>(path: string, init?: RequestInit, useCache: boolean = false): Promise<T> {
|
||||
if (useCache && init?.method === "GET") {
|
||||
try {
|
||||
return await getCachedData(path, () => fetchUncached<T>(path, init), CACHE_TTL);
|
||||
} catch (e) {
|
||||
console.error("Cache fetch failed, falling back to direct fetch:", e);
|
||||
return fetchUncached<T>(path, init);
|
||||
}
|
||||
}
|
||||
return fetchUncached<T>(path, init);
|
||||
}
|
||||
|
||||
async function fetchUncached<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
const token = getAuthToken();
|
||||
const headers: Record<string, string> = { "Content-Type": "application/json" };
|
||||
if (token) {
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
interface CacheEntry<T> {
|
||||
data: T;
|
||||
timestamp: number;
|
||||
ttl: number;
|
||||
}
|
||||
|
||||
class DataCache {
|
||||
private cache: Map<string, CacheEntry<any>>;
|
||||
private defaultTTL: number;
|
||||
|
||||
constructor(defaultTTL: number = 60000) {
|
||||
this.cache = new Map();
|
||||
this.defaultTTL = defaultTTL;
|
||||
}
|
||||
|
||||
set<T>(key: string, data: T, ttl?: number): void {
|
||||
const entry: CacheEntry<T> = {
|
||||
data,
|
||||
timestamp: Date.now(),
|
||||
ttl: ttl || this.defaultTTL,
|
||||
};
|
||||
this.cache.set(key, entry);
|
||||
}
|
||||
|
||||
get<T>(key: string): T | null {
|
||||
const entry = this.cache.get(key);
|
||||
if (!entry) return null;
|
||||
|
||||
const now = Date.now();
|
||||
if (now - entry.timestamp > entry.ttl) {
|
||||
this.cache.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry.data as T;
|
||||
}
|
||||
|
||||
invalidate(key: string): void {
|
||||
this.cache.delete(key);
|
||||
}
|
||||
|
||||
invalidatePattern(pattern: string): void {
|
||||
const regex = new RegExp(pattern);
|
||||
for (const key of this.cache.keys()) {
|
||||
if (regex.test(key)) {
|
||||
this.cache.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.cache.clear();
|
||||
}
|
||||
|
||||
size(): number {
|
||||
return this.cache.size;
|
||||
}
|
||||
}
|
||||
|
||||
export const cache = new DataCache();
|
||||
|
||||
export function getCachedData<T>(
|
||||
key: string,
|
||||
fetcher: () => Promise<T>,
|
||||
ttl?: number
|
||||
): Promise<T> {
|
||||
const cached = cache.get<T>(key);
|
||||
if (cached !== null) {
|
||||
return Promise.resolve(cached);
|
||||
}
|
||||
|
||||
return fetcher().then((data) => {
|
||||
cache.set(key, data, ttl);
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
export function invalidateCache(key: string): void {
|
||||
cache.invalidate(key);
|
||||
}
|
||||
|
||||
export function invalidateCachePattern(pattern: string): void {
|
||||
cache.invalidatePattern(pattern);
|
||||
}
|
||||
|
||||
export function clearCache(): void {
|
||||
cache.clear();
|
||||
}
|
||||
Reference in New Issue
Block a user