UNPKG

smart-cache-gc

Version:

A GC-aware cache using WeakRef and FinalizationRegistry

146 lines (99 loc) โ€ข 4.74 kB
# ๐Ÿ”’ SmartCache [![npm version](https://img.shields.io/npm/v/smart-cache-gc.svg)](https://www.npmjs.com/package/smart-cache-gc) [![bundlejs](https://deno.bundlejs.com/badge?q=smart-cache-gc)](https://bundlejs.com/?q=smart-cache-gc) [![CI](https://github.com/kingmaker9841/smart-resource-cache/actions/workflows/ci.yml/badge.svg)](https://github.com/kingmaker9841/smart-resource-cache/actions/workflows/ci.yml) > A garbage-collection-aware, WeakRef-powered cache for long-running apps. `SmartCache` is a memory-efficient in-memory cache built with `WeakRef` and `FinalizationRegistry`. It automatically cleans up values when they are garbage collected, helping you avoid memory leaks in high-uptime applications like servers, data processors, or editor tools. --- ## ๐Ÿš€ Features - ๐Ÿง  **Garbage collection-aware** with `WeakRef` and `FinalizationRegistry` - ๐Ÿ”‘ Supports both **object and symbol** values - ๐Ÿงผ Automatic cleanup of dead entries - ๐Ÿ” Utility methods: `.get()`, `.has()`, `.delete()`, `.clear()`, `.keys()`, `.size` - โณ Optional TTL (Time-To-Live) expiration - ๐Ÿ“‰ `maxSize` support with LRU eviction strategy - ๐Ÿงช Fully unit tested (with GC-safe behavior) --- ## ๐Ÿ“ฆ Installation ``` npm install smart-cache-gc ``` > `SmartCache` works out of the box with optional `ttl` and `maxSize` for expiration and LRU eviction. ## ๐Ÿ› ๏ธ Usage ``` import { SmartCache } from 'smart-cache-gc' // Optional TTL and maxSize support const cacheWithOptions = new SmartCache<object>({ defaultTtl: 1000 * 60, // 1 minute TTL by default maxSize: 100 // LRU eviction after 100 items }) let obj = { id: 1 } const key = { customKey: true } cache.set(key, obj) console.log(cache.has(key)) // true console.log(cache.get(key)) // { id: 1 } // Simulate garbage collection obj = null global.gc?.() setTimeout(() => { console.log(cache.get(key)) // null (after GC) }, 500) ``` ## ๐Ÿ“š Examples For comprehensive examples and real-world use cases, check out the examples directory in this repository: [Basic Usage](./src//examples/browser/main.ts) - Getting started with SmartCache [Advanced Configuration](./src/examples/node/index.ts) - Complex scenarios and best practices [Performance Benchmarks](./src/examples/node/index.ts) - Testing memory efficiency ## ๐Ÿ”” GC Cleanup Hook You can register a cleanup callback for when an object is garbage collected: ``` cache.getNotificationOnGC({ key, value: yourObject, cleanup: (key, value) => { console.log(`${String(key)} was garbage collected`) } }) ``` ## ๐Ÿงช Testing ``` node --expose-gc node_modules/vitest/vitest.mjs run ``` ## ๐Ÿ“ Methods | Method | Description | | --------------------- | -------------------------------------------- | | `set(key, value)` | Store a value under a key (object or symbol) | | `get(key)` | Get cached value or `null` if GC'd | | `has(key)` | Returns `true` if value is alive | | `delete(key)` | Removes an entry | | `clear()` | Clears all entries | | `keys()` | Returns all alive keys | | `size` (getter) | Number of alive entries | | `getNotificationOnGC` | Register a GC cleanup callback for a value | ## โš™๏ธ Constructor Options | Option | Description | | ------------ | ---------------------------------------------- | | `defaultTtl` | Default time-to-live for entries (in ms) | | `maxSize` | Maximum number of live entries before eviction | ## ๐Ÿ•’ ttl per-entry (override) You can override the default TTL on a per-entry basis using the optional ttl in .set(): ``` cache.set(key, value, { ttl: 5000 }) // expires in 5 seconds ``` This overrides the defaultTtl set in the constructor (if any) for this specific entry only. ## โ™ป๏ธ LRU Eviction When `maxSize` is defined, `SmartCache` uses an optimized LRU eviction policy to remove the least recently accessed entries. This keeps memory usage predictable in high-load scenarios. ## Support > โš ๏ธ This package requires support for `WeakRef` and `FinalizationRegistry`. These are available in: > > - Chrome 84+ > - Firefox 79+ > - Node.js 14.6+ > - Safari: Not yet supported as of 2025 > Please ensure your environment supports these features. ## ๐Ÿ“œ License [MIT](./LICENSE) ## ๐Ÿค Contributing Feel free to open issues or pull requests! ## Author Made with care by Milan Panta