mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-10-19 19:34:25 +00:00
- 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
89 lines
1.7 KiB
TypeScript
89 lines
1.7 KiB
TypeScript
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();
|
|
}
|