npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tlru

v1.0.2

Published

Time aware least recently used (TLRU) cache for Node

Downloads

1,467

Readme

codecov

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