55 lines
1.0 KiB
TypeScript
55 lines
1.0 KiB
TypeScript
/**
|
|
* Caching layer for Indie.fun bonding curve data
|
|
*/
|
|
|
|
interface CacheEntry<T> {
|
|
data: T;
|
|
timestamp: number;
|
|
ttl: number;
|
|
}
|
|
|
|
const cache = new Map<string, CacheEntry<any>>();
|
|
const DEFAULT_TTL = 5 * 60 * 1000; // 5 minutes
|
|
|
|
export function getCached<T>(key: string): T | null {
|
|
const entry = cache.get(key);
|
|
if (!entry) return null;
|
|
|
|
const now = Date.now();
|
|
if (now - entry.timestamp > entry.ttl) {
|
|
cache.delete(key);
|
|
return null;
|
|
}
|
|
|
|
return entry.data as T;
|
|
}
|
|
|
|
export function setCached<T>(key: string, data: T, ttl: number = DEFAULT_TTL): void {
|
|
cache.set(key, {
|
|
data,
|
|
timestamp: Date.now(),
|
|
ttl,
|
|
});
|
|
}
|
|
|
|
export function clearCache(key?: string): void {
|
|
if (key) {
|
|
cache.delete(key);
|
|
} else {
|
|
cache.clear();
|
|
}
|
|
}
|
|
|
|
// Cleanup old entries periodically
|
|
if (typeof window !== "undefined") {
|
|
setInterval(() => {
|
|
const now = Date.now();
|
|
for (const [key, entry] of cache.entries()) {
|
|
if (now - entry.timestamp > entry.ttl) {
|
|
cache.delete(key);
|
|
}
|
|
}
|
|
}, 60000); // Every minute
|
|
}
|
|
|