Skip to content

ahtrahdis7/node-lru-cache-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 !

About

An implementation of LRU Cache using Doubly Linked Lists and Maps with O(1) read and write time complexity. [500+ NPM Downloads]

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published