CacheNode API
Constructor
typescript
new CacheNode(id: string, config?: Partial<NodeConfig>)Tham số
| Param | Kiểu | Mặc định | Mô tả |
|---|---|---|---|
| id | string | bắt buộc | ID node duy nhất |
| config.maxSize | number | 1000 | Số entries tối đa trước khi eviction |
| config.defaultTtl | number | 60000 | TTL mặc định (ms) |
| config.evictionPolicy | "lru" | "lfu" | "fifo" | "lru" | Chiến lược eviction |
| config.sweepIntervalMs | number | 30000 | TTL sweep interval (0=tắt) |
| config.autoFlushPercent | number | 0 | Auto-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 nullset(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 5sdelete(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ônghas(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.