Time aware least recently used (TLRU) cache for Node

Time aware least recently used (TLRU) cache for Node

Time aware least recently used TLRU cache for Node.JS.Small (~ 70 lines of TypeScript), fast (extends native Map class, maintaining the same speed for set, get and has operations), memory efficient (don't uses an internal linked-list for LRU

tlru

Time aware least recently used TLRU cache for Node.JS

Small (~ 70 lines of TypeScript), fast (extends native Map class, maintaining the same speed for set, get and has operations), memory efficient (don't uses an internal linked-list for LRU, but instead uses just one timer per class instance via fun-dispatcher and proactively performs cache eviction - so great for caching large objects)

Usage as LRU cache

TLRU may be seen as regular LRU cache where all cached items have the same maxAge as whole cache, and, effectively, this library can be used as efficient LRU cache:

const { TLRU } = require('tlru')

const lru = new TLRU({ defaultLRU: true, maxStoreSize: 2 });

lru.set('a', 1);
lru.set('b', 2);
expect(lru.get('a')).toBe(1);
lru.delete('a');
lru.set('c', 1);
lru.set('d', 1);

// TLRU prunes cache lazily, so, need to give some time
setImmediate(() => {
  expect([...lru.keys()]).toEqual(['c', 'd']);
});

// Peek item without affecting it's LRU rating
lru.get('a', false /* this is the flag */);

// all native `Map` methods are here
lru.delete('a');
console.log([...lru.entries()]); // [[key, value], ...]

// Serializes to JSON, sorted by LRU rating
console.log(JSON.stringify(lru)); // [[key1, value1], [key2, value2], ...]

See more examples at __tests__/lru.test.ts

Usage as TLRU

Each item in cache can have its own TTU (time to usage or time to live).

const { TLRU } = require('tlru')

const lru = new TLRU({ maxStoreSize: 4, maxAgeMs: 1000 });

lru.set('a', 1, 500);
lru.set('b', 2, 700);
lru.set('c', 3); // default TTU = maxAgeMs

setTimeout(() => {
    expect(lru.has('a')).toBeFalsy();
    expect(lru.get('b')).toBe(2);
    expect(lru.has('c')).toBeTruthy();
    expect(lru.size).toBe(2);
}, 600);

setTimeout(() => {
    expect(lru.has('b')).toBeFalsy();
    expect(lru.has('c')).toBeTruthy();
    expect(lru.size).toBe(1);
}, 800);

Items can be revived to original TTU on getting, so, you will have LRU/TRLU hybrid:

const { TLRU } = require('tlru')

const lru = new TLRU({ maxStoreSize: 4, maxAgeMs: 1000 });
lru.set('a', 1, 500);
lru.set('b', 2, 700);
lru.set('c', 3); // default TTU = maxAgeMs

setTimeout(() => {
    expect(lru.has('a')).toBeFalsy();
    expect(lru.get('b', true /* revive flag - reset TTU */)).toBe(2); // 'b' got new 700 ms of life
    expect(lru.size).toBe(2);
}, 600);

setTimeout(() => {
    expect(lru.has('b')).toBeTruthy();
    expect(lru.has('c')).toBeFalsy(); // should be evicted by cache maxAgeMs default
    expect(lru.size).toBe(1);
}, 1200);

setTimeout(() => {
    expect(lru.size).toBe(0);
}, 1500);

Download Details:

Author: tinovyatkin

Source Code: https://github.com/tinovyatkin/tlru

nodejs node javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Node canvas is a Cairo backed Canvas implementation for NodeJS.

node-canvas is a Cairo-backed Canvas implementation for Node.js.

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.

Hire NodeJs Developer

Looking to build dynamic, extensively featured, and full-fledged web applications? **[Hire NodeJs Developer](https://hourlydeveloper.io/hire-dedicated-node-js-developer/ "Hire NodeJs Developer")** to create a real-time, faster, and scalable...

Decoding Nodejs

The main goal of this blog is to explain the “Architecture of Nodejs” and to know how the Nodejs works behind the scenes. Generally, most of the server-side languages, like PHP, ASP.NET, Ruby, and including Nodejs follows multi-threaded architecture. That means for each client-side request initiates a new thread or even a new process.

Node.js for Beginners - Learn Node.js from Scratch (Step by Step)

Node.js for Beginners - Learn Node.js from Scratch (Step by Step) - Learn the basics of Node.js. This Node.js tutorial will guide you step by step so that you will learn basics and theory of every part. Learn to use Node.js like a professional. You’ll learn: Basic Of Node, Modules, NPM In Node, Event, Email, Uploading File, Advance Of Node.