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
| Option | Type | Default | Description |
|---|---|---|---|
maxNodesCount | number | 1000 | Max nodes to cache |
maxEdgesCount | number | 2000 | Max 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 |
ttlSeconds | number | 3600 | Redis TTL (if using Redis) |
Eviction Strategies
| Strategy | Description |
|---|---|
LRU | Least Recently Used |
LFU | Least Frequently Used |
FIFO | First In, First Out |
Preload Strategies
| Strategy | Description |
|---|---|
'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
- Set appropriate limits — don't cache more than you need
- Use preload — if startup time isn't critical, preload on warmCache()
- Choose right eviction — LRU for temporal patterns, LFU for frequency
- Monitor hit rate — aim for >80% hit rate
Next Steps
- Storage Providers — pluggable backends
- API Reference — GraphManager API
- API Reference — CacheManager API