lru-cache-js-map
v1.0.7
Published
An implementation of LRU Cache using Doubly Linked Lists and Maps with O(1) read and write time.
Downloads
4
Maintainers
Readme
LRU Cache
Data Structures Used
- 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.
- 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
LRUCache.put(key, value)
: Add a key-value pair using this method.LRUCache.get(key)
: Returns the value for the key if present or returns-1
if not.LRUCache.remove(key)
: Removes a key value pair, if presentLRUCache.getCache()
: Fetches all the contents of the cache as a JSON Object.LRUCache.getKeysOfValue(val)
: Fetches all the keys with the provided value.LRUCache.getAllValues()
: Fetches all the values present in the Cache.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)