NLP-IBM-Debater / src /app /utils /cache.utils.ts
Yassine Mhirsi
enhance analysis page
78b3b2b
raw
history blame
4.14 kB
/**
* Cache utilities for storing and retrieving analysis data
*/
import type { AnalysisResult } from '../types/analysis.types.ts';
import type { MCPTool } from '../types/mcp.types.ts';
const CACHE_KEY = 'analysis_data_cache';
const CACHE_TIMESTAMP_KEY = 'analysis_data_cache_timestamp';
const MCP_TOOLS_CACHE_KEY = 'mcp_tools_cache';
const MCP_TOOLS_CACHE_TIMESTAMP_KEY = 'mcp_tools_cache_timestamp';
type CachedAnalysisData = {
results: AnalysisResult[];
timestamp: number;
};
type CachedMCPToolsData = {
tools: MCPTool[];
timestamp: number;
};
/**
* Cache analysis results to localStorage
*/
export function cacheAnalysisData(results: AnalysisResult[]): void {
try {
const cacheData: CachedAnalysisData = {
results,
timestamp: Date.now(),
};
localStorage.setItem(CACHE_KEY, JSON.stringify(cacheData));
localStorage.setItem(CACHE_TIMESTAMP_KEY, String(cacheData.timestamp));
} catch (error) {
// Silently fail if localStorage is not available or quota exceeded
console.warn('Failed to cache analysis data:', error);
}
}
/**
* Get cached analysis results from localStorage
*/
export function getCachedAnalysisData(): AnalysisResult[] | null {
try {
const cached = localStorage.getItem(CACHE_KEY);
if (!cached) return null;
const cacheData: CachedAnalysisData = JSON.parse(cached);
return cacheData.results;
} catch (error) {
// Silently fail if localStorage is not available or data is corrupted
console.warn('Failed to retrieve cached analysis data:', error);
return null;
}
}
/**
* Get cache timestamp
*/
export function getCacheTimestamp(): number | null {
try {
const timestamp = localStorage.getItem(CACHE_TIMESTAMP_KEY);
return timestamp ? Number(timestamp) : null;
} catch (error) {
return null;
}
}
/**
* Clear cached analysis data
*/
export function clearAnalysisCache(): void {
try {
localStorage.removeItem(CACHE_KEY);
localStorage.removeItem(CACHE_TIMESTAMP_KEY);
} catch (error) {
console.warn('Failed to clear analysis cache:', error);
}
}
export function cacheMcpTools(tools: MCPTool[]): void {
try {
const cacheData: CachedMCPToolsData = {
tools,
timestamp: Date.now(),
};
localStorage.setItem(MCP_TOOLS_CACHE_KEY, JSON.stringify(cacheData));
localStorage.setItem(
MCP_TOOLS_CACHE_TIMESTAMP_KEY,
String(cacheData.timestamp)
);
} catch (error) {
console.warn('Failed to cache MCP tools:', error);
}
}
export function getCachedMcpTools(): MCPTool[] | null {
try {
const cached = localStorage.getItem(MCP_TOOLS_CACHE_KEY);
if (!cached) return null;
const cacheData: CachedMCPToolsData = JSON.parse(cached);
return cacheData.tools;
} catch (error) {
console.warn('Failed to retrieve cached MCP tools:', error);
return null;
}
}
export function clearMcpToolsCache(): void {
try {
localStorage.removeItem(MCP_TOOLS_CACHE_KEY);
localStorage.removeItem(MCP_TOOLS_CACHE_TIMESTAMP_KEY);
} catch (error) {
console.warn('Failed to clear MCP tools cache:', error);
}
}
/**
* Cache recent analysis results to localStorage
*/
export function cacheRecentAnalysisData(results: AnalysisResult[]): void {
try {
const cacheData: CachedAnalysisData = {
results,
timestamp: Date.now(),
};
localStorage.setItem('recent_analysis_data_cache', JSON.stringify(cacheData));
} catch (error) {
// Silently fail if localStorage is not available or quota exceeded
console.warn('Failed to cache recent analysis data:', error);
}
}
/**
* Get cached recent analysis results from localStorage
*/
export function getCachedRecentAnalysisData(): AnalysisResult[] | null {
try {
const cached = localStorage.getItem('recent_analysis_data_cache');
if (!cached) return null;
const cacheData: CachedAnalysisData = JSON.parse(cached);
return cacheData.results;
} catch (error) {
// Silently fail if localStorage is not available or data is corrupted
console.warn('Failed to retrieve cached recent analysis data:', error);
return null;
}
}