super-lru-cache
v1.0.0
Published
A super fast LRU Cache implementation using Doubly Linked List and Hash Map. O(1) time complexity.
Downloads
78
Maintainers
Readme
⚡ Super Fast LRU Cache
A high-performance, lightweight implementation of the Least Recently Used (LRU) Cache algorithm in JavaScript.
Built using a combination of Doubly Linked List and Hash Map, ensuring that both read and write operations happen in O(1) (Constant Time). Perfect for caching API responses, managing browser history, or optimizing heavy computations.
🚀 Features
- ⚡ Blazing Fast:
getandputoperations perform in O(1) time complexity. - 📦 Ultra Lightweight: Zero external dependencies.
- 🧠 Efficient Memory Management: Automatically removes the least recently used items when capacity is reached.
- ✅ Easy to Use: Simple and intuitive API.
📦 Installation
You can install this package via npm:
npm install super-lru-cache
🛠 Usage
Here is a quick example of how to use the cache:
const LRUCache = require('super-lru-cache');
// 1. Initialize the cache with a capacity of 3 items
const cache = new LRUCache(3);
// 2. Add items to the cache
cache.put("user_1", "Rohit");
cache.put("user_2", "Virat");
cache.put("user_3", "Dhoni");
console.log(cache.get("user_1"));
// Output: "Rohit"
// (user_1 is now the "Most Recently Used" item)
// 3. Add a new item (Cache is full!)
// This will trigger the eviction of 'user_2' (Virat) because 'user_1' was just accessed
// and 'user_2' became the least recently used.
cache.put("user_4", "Hardik");
// 4. Try to access the evicted item
console.log(cache.get("user_2"));
// Output: -1 (Item was removed)
// 5. Check the current state
// Cache contains: [Hardik, user_1, user_3]
Bilkul, yeh lijiye saari files ek jagah. Aapko bas 2 files update karni hain aur phir publish command chalana hai.
File 1: package.json
(Is file mein poora purana code hata kar ye paste karein. Maine version 1.0.1 kar diya hai aur keywords add kar diye hain).
JSON
{
"name": "super-lru-cache",
"version": "1.0.1",
"description": "A high-performance, O(1) time complexity LRU Cache implementation using Doubly Linked List and Hash Map.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"lru",
"cache",
"lru-cache",
"linked-list",
"data-structure",
"fast",
"optimization",
"performance",
"javascript"
],
"author": "Rohit Kumar",
"license": "MIT"
}
File 2: README.md
(Is file mein purana content hata kar ye paste karein).
Markdown
# ⚡ Super Fast LRU Cache
> A high-performance, lightweight implementation of the Least Recently Used (LRU) Cache algorithm in JavaScript.
[](https://www.npmjs.com/package/super-lru-cache)
Built using a combination of **Doubly Linked List** and **Hash Map**, ensuring that both read and write operations happen in **O(1)** (Constant Time). Perfect for caching API responses, managing browser history, or optimizing heavy computations.
---
## 🚀 Features
- **⚡ Blazing Fast:** `get` and `put` operations perform in **O(1)** time complexity.
- **📦 Ultra Lightweight:** Zero external dependencies.
- **🧠 Efficient Memory Management:** Automatically removes the least recently used items when capacity is reached.
- **✅ Easy to Use:** Simple and intuitive API.
---
## 📦 Installation
You can install this package via npm:
```bash
npm install super-lru-cache
🛠 Usage
Here is a quick example of how to use the cache:
JavaScript
const LRUCache = require('super-lru-cache');
// 1. Initialize the cache with a capacity of 3 items
const cache = new LRUCache(3);
// 2. Add items to the cache
cache.put("user_1", "Rohit");
cache.put("user_2", "Virat");
cache.put("user_3", "Dhoni");
console.log(cache.get("user_1"));
// Output: "Rohit"
// (user_1 is now the "Most Recently Used" item)
// 3. Add a new item (Cache is full!)
// This will trigger the eviction of 'user_2' (Virat) because 'user_1' was just accessed
// and 'user_2' became the least recently used.
cache.put("user_4", "Hardik");
// 4. Try to access the evicted item
console.log(cache.get("user_2"));
// Output: -1 (Item was removed)
// 5. Check the current state
// Cache contains: [Hardik, user_1, user_3]
📚 API Reference
new LRUCache(capacity)
Creates a new cache instance.
capacity (number): The maximum number of items the cache can hold.
.put(key, value)
Adds a key-value pair to the cache. If the key already exists, its value is updated and it becomes the most recently used item. If the cache is full, the least recently used item is removed.
.get(key)
Retrieves the value associated with the key.
Returns the value if the key exists.
Returns -1 if the key does not exist.
Side Effect: Moves the accessed item to the "Most Recently Used" position.
🤝 Contributing
Contributions, issues, and feature requests are welcome!
📝 Author
Rohit Kumar
📄 License
This project is MIT licensed.