LRU is using Map as underlying cache.
Import LRU and create new LRU object
// From deno.land
import { LRU } from "https://deno.land/x/lru@1.0.2/mod.ts";
import LRU from "https://deno.land/x/lru@1.0.2/mod.ts";
//From nest.land
import { LRU } from "https://x.nest.land/LRU@1.0.2/mod.ts";
import LRU from "https://x.nest.land/LRU@1.0.2/mod.ts";
const lru = new LRU(500); // define your max amount of entries, in this example is 500
Methods to get,set and remove key:value pair.
lru.set("key", "value");
lru.get("key");
lru.remove("key");
Returns true if an element with the specified key exists in the cache otherwise false
lru.has("key");
Returns amount of entries in the cache
lru.size;
Returns entries as object
lru.object;
Returns array of all keys
lru.keys;
Returns array of all values
lru.values;
Executes a provided function once for each array element
lru.forEach((value, key) => {
// code
});
Method creates a new array populated with the results of calling a provided function on every element in the calling array
lru.map(([key, value]) => {
// code
});
Creates a new array with all elements that pass the test implemented by the provided function
lru.filter(([key, value]) => {
// code
});
Executes a reducer function (that you provide) on each element of the array, resulting in single output value
lru.reduce((previousValue, currentValue, currentIndex) => {
// code
}, initialValue);
for (const [key, value] of lru) {
// code
}
Removes all entries in the cache
lru.clear();
Author: liltoto
Source Code: https://github.com/liltoto/LRU
#deno #nodejs #node #javascript