30 lines
686 B
TypeScript
30 lines
686 B
TypeScript
// src/utils/cacheStore.ts
|
|
interface CacheItem<T> {
|
|
data: T;
|
|
timestamp: number;
|
|
}
|
|
|
|
const TTL = 1 * 1000; // dữ liệu có hiệu lực trong 1 giây để GPS realtime
|
|
const cache: Record<string, CacheItem<any>> = {};
|
|
|
|
export function getCache<T>(key: string): T | null {
|
|
const item = cache[key];
|
|
if (!item) return null;
|
|
if (Date.now() - item.timestamp > TTL) {
|
|
delete cache[key];
|
|
return null;
|
|
}
|
|
return item.data;
|
|
}
|
|
|
|
export function setCache<T>(key: string, data: T) {
|
|
cache[key] = { data, timestamp: Date.now() };
|
|
}
|
|
|
|
export function invalidate(key: string) {
|
|
delete cache[key];
|
|
}
|
|
|
|
export function getAllCacheKeys(): string[] {
|
|
return Object.keys(cache);
|
|
} |