@feizk/cache
v1.2.3
Published
Type-safe, multi-backend caching library for Node.js
Maintainers
Readme
@feizk/cache
Type-safe, multi-backend caching library for Node.js
Supports Memory (LRU) and Redis backends with full generic types, TTL, metrics, and more.
✨ Features
- 🔒 Fully typed – Generic
Cache<T>with noany - ⚡ Multiple backends – Memory (LRU) & Redis out of the box
- ⏱️ TTL support – Per-key or default expiration
- 📊 Metrics – Hit/miss rates, operation durations
- 🔄 Bulk ops –
getMany,setMany,deleteMany - 🎯 Cache-aside –
getOrFetchfor lazy loading - 🏷️ Namespaces – Key prefixing for multi-tenant apps
- 🧠 Smart serialization – Handles Date, Map, Set, Buffer, RegExp automatically
📦 Installation
pnpm add @feizk/cache
# or
npm install @feizk/cache🚀 Quick Start
Memory Backend (LRU)
import { Cache, MemoryBackend } from '@feizk/cache';
const cache = new Cache<string>({
backend: new MemoryBackend({ maxEntries: 1000 }),
defaultTtl: 5 * 60 * 1000, // 5 minutes
});
await cache.set('key', 'hello');
const value = await cache.get('key'); // 'hello' | nullRedis Backend
import { Cache, RedisBackend } from '@feizk/cache';
const cache = new Cache<{ id: number; name: string }>({
backend: new RedisBackend({ url: 'redis://localhost:6379' }),
namespace: 'myapp',
defaultTtl: 10 * 60 * 1000, // 10 minutes
});
const user = await cache.getOrFetch(
`user:${id}`,
async () => await db.users.findById(id),
);📚 Documentation
For detailed API reference, backend configuration, serialization, metrics, FAQ, and troubleshooting, check the docs/ directory:
- Getting Started – Installation and basic usage
- API Reference – All classes, methods, and options
- Backends – Memory vs Redis, when to use which
- Serialization – How types are preserved
- Metrics – Monitoring cache performance
- FAQ – Frequently asked questions
- Troubleshooting – Common issues and solutions
🤝 Contributing
Found a bug or have a suggestion? Please open an issue on GitHub.
📄 License
MIT © feizk
