Simple in-memory cache implementation
npm install fast-memory-cache!npm
!npm bundle size
!npm
!license
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
``bash`
npm install fast-memory-cacheor
yarn add fast-memory-cacheor
pnpm add fast-memory-cache
`ts
import MemoryCache from 'fast-memory-cache';
interface User {
id: number;
name: string;
}
const cache = new MemoryCache();
// Type-safe storage and retrieval
cache.set
const user = cache.get
// With expiration
cache.set
`
`js
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();
`
`ts`
const cache = new MemoryCache();
#### get
Retrieves a value from the cache.
- Returns undefined if the key doesn't exist or has expired
#### set
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)0
- Set to or omit for values that never expire
#### delete(key: string): void
Removes a value from the cache.
#### clear(): void`
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