Lightweight, extensible, fully typed in‑memory cache for TypeScript / Node.js.
TTL · size limit · pluggable strategies (LRU / custom) · fetch missing values · typed event hooks.
Polska wersja:
Readme_PL.mdorazdocs/pl/*.
@tfxjs/cache = minimal core + extension surface:
- Size limit & strategy eviction (Base / LRU / custom)
- Global & per‑item TTL + optional background cleanup
- Fetchable mode (
CreateFetchable*factories) for automatic miss fill - Strongly typed events mapped to optional strategy hook methods
- Simple, testable, no global emitter overhead
npm install @tfxjs/cache
# or
yarn add @tfxjs/cacheimport { CreateStandardLRUCache } from '@tfxjs/cache';
const cache = CreateStandardLRUCache<string>({ maxSize: 100, ttl: 30_000, cleanupInterval: 5_000 });
cache.setCacheItem('user:1', 'John');
console.log(cache.getFromCache('user:1')); // John
cache.dispose(); // stops background timers; create a new instance to use cache againFetcher + LRU:
import { CreateFetchableLRUCache, TFetchStrategy } from '@tfxjs/cache';
class UserFetcher implements TFetchStrategy<string> {
async fetch(key: string) {
return key === 'u:1' ? 'John' : null;
}
}
const cache = CreateFetchableLRUCache<string>({ maxSize: 50, ttl: 20_000 }, new UserFetcher());
await cache.getOrFetch('u:1');See detailed docs under docs/en/:
| Section | File |
|---|---|
| Core (API, options, events) | docs/en/Cache.md |
| Strategies (interface, hooks, LRU, custom) | docs/en/Strategies.md |
| Fetchers (interface, examples) | docs/en/Fetchers.md |
MIT © tfxjs
Questions / ideas? Open an issue or PR. Enjoy caching! 🧠