Skip to main content

CacheManager

Manages cache across multiple graphId partitions with budget enforcement.

Import

import { CacheManager } from 'grafio';

getStats()

static getStats(graphId: string): CacheStats

Get cache statistics for a specific graph.

CacheStats

interface CacheStats {
hits: number; // Total cache hits
misses: number; // Total cache misses
hitRate: number; // Hit rate as percentage
nodesCount: number; // Current cached nodes
edgesCount: number; // Current cached edges
}

Example

import { CacheManager } from 'grafio';

const stats = CacheManager.getStats('default');
console.log(`Hits: ${stats.hits}, Misses: ${stats.misses}`);
console.log(`Hit rate: ${stats.hitRate.toFixed(2)}%`);
console.log(`Nodes: ${stats.nodesCount}, Edges: ${stats.edgesCount}`);

clearCache()

static clearCache(graphId: string): void

Clear cache for a specific graph.

CacheManager.clearCache('default');

clearAllCaches()

static clearAllCaches(): void

Clear cache for all graphs.

CacheManager.clearAllCaches();

setBudget()

static setBudget(graphId: string, budget: { maxNodes?: number; maxEdges?: number }): void

Update budget for a specific graph.

CacheManager.setBudget('default', { maxNodes: 20000, maxEdges: 40000 });

getBudget()

static getBudget(graphId: string): { maxNodes: number; maxEdges: number } | undefined

Get current budget for a graph.

const budget = CacheManager.getBudget('default');
if (budget) {
console.log(`Max nodes: ${budget.maxNodes}`);
console.log(`Max edges: ${budget.maxEdges}`);
}

Events

The CacheManager emits events for monitoring:

cacheManager.on('hit', ({ graphId, key }) => { /* ... */ });
cacheManager.on('miss', ({ graphId, key }) => { /* ... */ });
cacheManager.on('eviction', ({ graphId, key, reason }) => { /* ... */ });

Next Steps