Simple and Powerful LRU Cache for Deno

LRU

LRU is using Map as underlying cache.

Usage

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

Set, get and remove

Methods to get,set and remove key:value pair.

lru.set("key", "value");
lru.get("key");
lru.remove("key");

Has

Returns true if an element with the specified key exists in the cache otherwise false

lru.has("key");

Size

Returns amount of entries in the cache

lru.size;

Object

Returns entries as object

lru.object;

Keys

Returns array of all keys

lru.keys;

Values

Returns array of all values

lru.values;

ForEach

Executes a provided function once for each array element

lru.forEach((value, key) => {
  // code
});

Map

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
});

Filter

Creates a new array with all elements that pass the test implemented by the provided function

lru.filter(([key, value]) => {
  // code
});

Reduce

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…of

for (const [key, value] of lru) {
  // code
}

Clear

Removes all entries in the cache

lru.clear();

Download Details:

Author: liltoto

Source Code: https://github.com/liltoto/LRU

#deno #nodejs #node #javascript

Simple and Powerful LRU Cache for Deno
16.90 GEEK