Skip to content

CacheNode API

Constructor

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

Tham số

ParamKiểuMặc địnhMô tả
idstringbắt buộcID node duy nhất
config.maxSizenumber1000Số entries tối đa trước khi eviction
config.defaultTtlnumber60000TTL mặc định (ms)
config.evictionPolicy"lru" | "lfu" | "fifo""lru"Chiến lược eviction
config.sweepIntervalMsnumber30000TTL sweep interval (0=tắt)
config.autoFlushPercentnumber0Auto-flush threshold (0=tắt)
config.onEvicted(key: string) => void-Callback khi evict

Phương thức

get(key: string): Value | null

Lấy value từ cache.

typescript
const value = node.get("user:123");
// Trả về: { name: "John" } hoặc null

set(key: string, value: Value, ttl?: number): void

Lưu value vào cache.

typescript
node.set("user:123", { name: "John" });
node.set("temp:data", "value", 5000); // TTL 5s

delete(key: string): boolean

Xóa key khỏi cache.

typescript
const deleted = node.delete("user:123");
// Trả về: true nếu tồn tại, false nếu không

has(key: string): boolean

Kiểm tra key tồn tại (và chưa hết hạn).

typescript
if (node.has("user:123")) {
  // key tồn tại và hợp lệ
}

clear(): number

Xóa tất cả entries. Trả về bytes đã giải phóng.

typescript
const freed = node.clear();
console.log(`Đã giải phóng ${freed} bytes`);

getKeys(): string[]

Lấy tất cả keys trong node.

typescript
const keys = node.getKeys();
// Trả về: ["user:123", "product:456", ...]

getSize(): number

Số entries hiện tại.

getMaxSize(): number

Số entries tối đa cho phép.

enablePersistence(config: FileStorageConfig): void

Bật lưu trữ dựa trên file.

loadFromDisk(): boolean

Tải dữ liệu từ file.

saveToDisk(): boolean

Lưu dữ liệu xuống file.

Released under the MIT License.