@se-oss/simple-lru
v1.0.0
Published
A lightweight, high-performance Least Recently Used (LRU) cache
Downloads
146
Readme
@se-oss/simple-lru is a fast, lightweight, and type-safe Least Recently Used (LRU) cache. It implements standard Map-like behavior with auto-eviction of oldest items.
📦 Installation
pnpm add @se-oss/simple-lrunpm
npm install @se-oss/simple-lruyarn
yarn add @se-oss/simple-lru📖 Usage
Basic Usage
import { SimpleLRU } from '@se-oss/simple-lru';
const cache = new SimpleLRU<string, number>(100);
cache.set('key', 42);
console.log(cache.get('key')); // 42Eviction Callback
const cache = new SimpleLRU<string, number>(2, {
onEvict(key, value) {
console.log(`Evicted: ${key} = ${value}`);
},
});
cache.set('a', 1);
cache.set('b', 2);
cache.set('c', 3); // Evicts 'a' and calls onEvict('a', 1)Checking Existence
Check if a key exists in the cache without updating its insertion order.
if (cache.has('key')) {
// ...
}Iteration
Standard iterator support, ordered from least recently used (LRU) to most recently used (MRU).
for (const [key, value] of cache) {
console.log(key, value);
}📚 Documentation
For all configuration options, please see the API docs.
🤝 Contributing
Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.
Thanks again for your support, it is much appreciated! 🙏
License
MIT © Shahrad Elahi and contributors.
