Skip to content

CacheNode API

Constructor

typescript
new CacheNode(id: string, config?: Partial<NodeConfig>)

Parameters

ParamTypeDefaultDescription
idstringrequiredUnique node identifier
config.maxSizenumber1000Max entries before eviction
config.defaultTtlnumber60000Default TTL in ms
config.evictionPolicy"lru" | "lfu" | "fifo""lru"Eviction strategy
config.sweepIntervalMsnumber30000TTL sweep interval (0=off)
config.autoFlushPercentnumber0Auto-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 null

set(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 TTL

delete(key: string): boolean

Remove key from cache.

typescript
const deleted = node.delete("user:123");
// Returns: true if existed, false otherwise

has(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.

Released under the MIT License.