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-cacheimportMemoryCachefrom'fast-memory-cache';interfaceUser{id: number;name: string;}constcache=newMemoryCache();// Type-safe storage and retrievalcache.set<User>('user',{id: 1,name: 'John'});constuser=cache.get<User>('user');// User type// With expirationcache.set<string>('token','abc123',300);// expires in 5 minutesconstMemoryCache=require('fast-memory-cache');// Create a new cache instanceconstcache=newMemoryCache();// Store a valuecache.set('user',{id: 1,name: 'John'});// Retrieve a valueconstuser=cache.get('user');// { id: 1, name: 'John' }// Store with expiration (in seconds)cache.set('session','abc123',60);// expires in 60 seconds// Delete a valuecache.delete('user');// Clear all cachecache.clear();constcache=newMemoryCache();Retrieves a value from the cache.
- Returns
undefinedif 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
0or 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