Skip to main content

CacheConfig

Configuration options for the caching layer.

Import

import type { CacheConfig, EvictionStrategy, PreloadStrategy, CacheStoreType } from 'grafio';

Interface

interface CacheConfig {
maxNodesCount: number; // Maximum nodes to cache
maxEdgesCount: number; // Maximum edges to cache
cacheStore: CacheStoreType; // Cache backend
evictionStrategy: EvictionStrategy; // Eviction algorithm
preloadStrategy: PreloadStrategy; // Preload strategy
ttlSeconds?: number; // Redis TTL (if using Redis)
}

EvictionStrategy

type EvictionStrategy = 'LRU' | 'LFU' | 'FIFO';
StrategyFull NameDescription
'LRU'Least Recently UsedEvicts least recently accessed items
'LFU'Least Frequently UsedEvicts least frequently accessed items
'FIFO'First In, First OutEvicts oldest items first

Recommendation:

  • LRU — for temporal access patterns
  • LFU — for frequency-based access
  • FIFO — for simple caching needs

PreloadStrategy

type PreloadStrategy = 'none' | 'all' | 'first-n';
StrategyDescription
'none'No preload (default)
'all'Preload all existing nodes and edges
'first-n'Preload first N items based on storage order

Recommendation:

  • 'none' — for large graphs or memory constraints
  • 'all' — for small graphs that fit in memory
  • 'first-n' — for incremental warming

CacheStoreType

type CacheStoreType = 'in-memory' | 'redis';
TypeDescriptionUse Case
'in-memory'In-process LRU cacheSingle instance, development
'redis'Redis distributed cacheMulti-instance, production

Example Configuration

import { GraphManager, InMemoryStorageProvider, CacheConfig } from 'grafio';

// Development configuration
const devConfig: CacheConfig = {
maxNodesCount: 10000,
maxEdgesCount: 20000,
cacheStore: 'in-memory',
evictionStrategy: 'LRU',
preloadStrategy: 'none',
};

// Production configuration with Redis
const prodConfig: CacheConfig = {
maxNodesCount: 100000,
maxEdgesCount: 500000,
cacheStore: 'redis',
evictionStrategy: 'LFU',
preloadStrategy: 'all',
ttlSeconds: 7200, // 2 hours
};

GraphManager.init({ cache: devConfig });

Default Configuration

const DEFAULT_CACHE_CONFIG: CacheConfig = {
maxNodesCount: 10000,
maxEdgesCount: 20000,
cacheStore: 'in-memory',
evictionStrategy: 'LRU',
preloadStrategy: 'none',
};

Next Steps