Skip to main content

Caching

Improve read performance with Grafio's pluggable caching layer.

Overview

Quick Start

import { GraphManager, InMemoryGraphFactory } from 'grafio';

// 1. Initialize GraphManager with cache config
GraphManager.init({
cache: {
maxNodesCount: 1000,
maxEdgesCount: 2000,
cacheStore: 'in-memory',
evictionStrategy: 'LRU',
preloadStrategy: 'none',
}
});

// 2. Create graph via factory
const factory = new InMemoryGraphFactory();
const graph = factory.forGraph('default');

Cache Configuration

CacheConfig Options

OptionTypeDefaultDescription
maxNodesCountnumber1000Max nodes to cache
maxEdgesCountnumber2000Max edges to cache
cacheStore'in-memory' | 'redis''in-memory'Cache backend
evictionStrategy'LRU' | 'LFU' | 'FIFO''LRU'Eviction algorithm
preloadStrategy'none' | 'all' | 'first-n''none'Preload strategy
ttlSecondsnumber3600Redis TTL (if using Redis)

Eviction Strategies

StrategyDescription
LRULeast Recently Used
LFULeast Frequently Used
FIFOFirst In, First Out

Preload Strategies

StrategyDescription
'none'No preload (default)
'all'Preload all nodes and edges
'first-n'Preload first N items

Using Redis Cache

import { GraphManager, RedisCache } from 'grafio/cache';

GraphManager.init({
cache: {
cacheStore: 'redis',
maxNodesCount: 5000,
maxEdgesCount: 10000,
evictionStrategy: 'LRU',
preloadStrategy: 'all',
ttlSeconds: 7200,
}
});

Cache Statistics

Monitor cache performance:

const stats = CacheManager.getStats(graphId);
// stats.hits, stats.misses, stats.size, etc.

CacheManager API

import { CacheManager, CacheStats } from 'grafio';

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

Performance Tips

  1. Set appropriate limits — don't cache more than you need
  2. Use preload — if startup time isn't critical, preload on warmCache()
  3. Choose right eviction — LRU for temporal patterns, LFU for frequency
  4. Monitor hit rate — aim for >80% hit rate

Next Steps