helpers.ts•1.16 kB
// *****************************************************
// *********** Hacker News MCP Helpers ***********
// *****************************************************
const cache = new Map<string, { data: any; timestamp: number }>();
const CACHE_DURATION = 2 * 60 * 1000; // 2 minutes
// Function to get cached data
export function getCached<T>(key: string): T | null {
const cached = cache.get(key);
if (cached && Date.now() - cached.timestamp < CACHE_DURATION) {
return cached.data;
}
return null;
}
// Function to set cached data
export function setCache(key: string, data: any): void {
cache.set(key, { data, timestamp: Date.now() });
}
// Function to format timestamp to a readable format
export function formatTime(timestamp: number): string {
const date = new Date(timestamp * 1000);
const now = new Date();
const diff = now.getTime() - date.getTime();
const minutes = Math.floor(diff / (1000 * 60));
const hours = Math.floor(diff / (1000 * 60 * 60));
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
if (minutes < 60) return `${minutes}m ago`;
if (hours < 24) return `${hours}h ago`;
return `${days}d ago`;
}