https://github.com/ahtrahdis7/node-lru-cache-js
An implementation of LRU Cache using Doubly Linked Lists and Maps with O(1) read and write time complexity. [500+ NPM Downloads]
https://github.com/ahtrahdis7/node-lru-cache-js
cache data-structures lru-cache lru-implementation nodejs
Last synced: 28 days ago
JSON representation
An implementation of LRU Cache using Doubly Linked Lists and Maps with O(1) read and write time complexity. [500+ NPM Downloads]
- Host: GitHub
- URL: https://github.com/ahtrahdis7/node-lru-cache-js
- Owner: ahtrahdis7
- Created: 2021-05-31T04:43:44.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-09-09T14:40:18.000Z (over 3 years ago)
- Last Synced: 2025-03-27T12:12:27.823Z (about 2 months ago)
- Topics: cache, data-structures, lru-cache, lru-implementation, nodejs
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/lru-cache-js-map
- Size: 214 KB
- Stars: 14
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# LRU Cache
## Data Structures Used
1. Queue which is implemented using a doubly linked list. The maximum size of the queue will be equal to the total number of frames available (cache size). The most recently used pages will be near front end and least recently pages will be near the rear end.
2. A Hash with page number as key and address of the corresponding queue node as value.### If you find this piece of work interesting, dont forget to give a `star` on github.
## Methods
1. `LRUCache.put(key, value)` : Add a key-value pair using this method.
2. `LRUCache.get(key)` : Returns the value for the key if present or returns `-1` if not.
3. `LRUCache.remove(key)` : Removes a key value pair, if present
4. `LRUCache.getCache()` : Fetches all the contents of the cache as a JSON Object.
5. `LRUCache.getKeysOfValue(val)` : Fetches all the keys with the provided value.
6. `LRUCache.getAllValues()` : Fetches all the values present in the Cache.
7. `LRUCache.getAllKeys()` : Fetches all the keys present in the Cache.## Implementation
```
npm i lru-cache-js-map
``````
const LRUCache = require('lru-cache-js-map');var cache = new LRUCache(100);
for(var i=0; i < 100; i++){
cache.put(Math.floor(Math.random()*10), Math.random()*10000);
}console.log(cache)
```## ! Happy Coding !