@aegenet/belt-memory-cache
v2.3.0
Published
Basic memory cache with TTL
Downloads
99
Readme
@aegenet/belt-memory-cache
Basic memory cache with TTL
💾 Installation
yarn add @aegenet/belt-memory-cache@^2.0.0
# or
npm i @aegenet/belt-memory-cache@^2.0.0
📝 Usage
import { MemoryCache } from '@aegenet/belt-memory-cache';
const cache = new MemoryCache({
// The interval in minutes at which the cache will be cleaned up. -> default: 5
cleanupIntervalMinutes: 5,
// If true, values will be cloned before being stored in the cache. -> default: true
cloneValues: true,
// Convert the key before storing it in the cache. -> default: (key) => key
convertKey: (key: unknown) => key,
});
// The cache will remove all expired items every 5 minutes
cache.start();
cache.set('key', 'value', 1); // 1000ms
cache.get('key'); // 'value'
await setTimeout(1000);
cache.get('key'); // undefined
// Stop the cache cleanup
cache.stop();