A lightweight, high-performance in-memory cache for Node.js applications with TypeScript support.
- 💨 Fast: Optimized for rapid access and storage
- 🔑 Simple API: Intuitive methods for cache operations
- ⏱️ TTL Support: Configurable time-to-live for cached items
- 🧠 Memory Efficient: Minimal overhead for stored values
- 📦 Zero Dependencies: No external packages required
- 📝 TypeScript Support: Full type definitions included
npm install fast-memory-cache
# or
yarn add fast-memory-cache
# or
pnpm add fast-memory-cache
import MemoryCache from 'fast-memory-cache';
interface User {
id: number;
name: string;
}
const cache = new MemoryCache();
// Type-safe storage and retrieval
cache.set<User>('user', { id: 1, name: 'John' });
const user = cache.get<User>('user'); // User type
// With expiration
cache.set<string>('token', 'abc123', 300); // expires in 5 minutes
const MemoryCache = require('fast-memory-cache');
// Create a new cache instance
const cache = new MemoryCache();
// Store a value
cache.set('user', { id: 1, name: 'John' });
// Retrieve a value
const user = cache.get('user'); // { id: 1, name: 'John' }
// Store with expiration (in seconds)
cache.set('session', 'abc123', 60); // expires in 60 seconds
// Delete a value
cache.delete('user');
// Clear all cache
cache.clear();
const cache = new MemoryCache();
Retrieves a value from the cache.
- Returns
undefined
if the key doesn't exist or has expired
Stores a value in the cache.
key
: The cache keyvalue
: The value to storeexpireTime
: Time in seconds after which the value will be automatically deleted (optional)- Set to
0
or omit for values that never expire
- Set to
Removes a value from the cache.
Removes all values from the cache.
Fast Memory Cache is designed to be extremely lightweight and performant:
- No serialization/deserialization overhead
- Direct in-memory storage with no I/O operations
- Efficient timeout management for expiration
Fast Memory Cache is ideal for:
- Caching API responses
- Storing session data
- Memoizing expensive function results
- Reducing database load
- Any scenario requiring fast, temporary data storage
MIT
If you find this package useful, consider:
- ⭐ Starring the repository
- 🐛 Reporting issues
- 🤝 Contributing with PRs