CacheNode API
Constructor
typescript
new CacheNode(id: string, config?: Partial<NodeConfig>)Parameters
| Param | Type | Default | Description |
|---|---|---|---|
| id | string | required | Unique node identifier |
| config.maxSize | number | 1000 | Max entries before eviction |
| config.defaultTtl | number | 60000 | Default TTL in ms |
| config.evictionPolicy | "lru" | "lfu" | "fifo" | "lru" | Eviction strategy |
| config.sweepIntervalMs | number | 30000 | TTL sweep interval (0=off) |
| config.autoFlushPercent | number | 0 | Auto-flush threshold (0=off) |
| config.onEvicted | (key: string) => void | - | Callback on eviction |
Methods
get(key: string): Value | null
Retrieve value from cache.
typescript
const value = node.get("user:123");
// Returns: { name: "John" } or nullset(key: string, value: Value, ttl?: number): void
Store value in cache.
typescript
node.set("user:123", { name: "John" });
node.set("temp:data", "value", 5000); // 5s TTLdelete(key: string): boolean
Remove key from cache.
typescript
const deleted = node.delete("user:123");
// Returns: true if existed, false otherwisehas(key: string): boolean
Check if key exists (and not expired).
typescript
if (node.has("user:123")) {
// key exists and is valid
}clear(): number
Clear all entries. Returns bytes freed.
typescript
const freed = node.clear();
console.log(`Freed ${freed} bytes`);getKeys(): string[]
Get all keys in this node.
typescript
const keys = node.getKeys();
// Returns: ["user:123", "product:456", ...]getSize(): number
Current number of entries.
getMaxSize(): number
Maximum entries allowed.
enablePersistence(config: FileStorageConfig): void
Enable file-based persistence.
loadFromDisk(): boolean
Load data from file.
saveToDisk(): boolean
Save data to file.