lib-lru-cache

LRU Cache

Usage no npm install needed!

<script type="module">
  import libLruCache from 'https://cdn.skypack.dev/lib-lru-cache';
</script>

README

lib-lru-cache Build Status Maintainability

A blazing fast & light weight LRU Cache Implementation in nodejs.

Installation

npm i lib-lru-cache -S

Methods

new LRUCache(maxLimit = 100)
Creates an LRUCache with Max Limit in size equals to maxLimit. defaults to 100
Please note that you cannot use the LRUCache if the maxLimit is more than 100, as that might hog up a lot of RAM

set('name', 'test')
(key, val) Save key => val pair and if the maxLimit of the Cache has been exceeded, then the First-Used is removed.
Please note that if the same key is set twice, then the previously set value is over-ridden

get('name')
(key) Returns the value of the corresponding key if available, else return null

reset()
Removes all the data in the LRUCache

Usage

const cache = new LRUCache(2)
cache.set('name1', 'test1')
cache.set('name2', 'test2')
cache.set('name3', 'test3')

console.log(cache.get('name3')) // test3
console.log(cache.get('name2')) // test2
console.log(cache.get('name1')) // null

cache.reset()
console.log(cache.get('name3')) // null

Test

npm test

Coverage

npm run coverage

Benchmark

npm run benchmark

Contributions

This is open-source, which makes it obvious for any PRs, but I would request you to add necessary test-cases for the same