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

@mobilabs/lru

v1.0.6

Published

An in-memory key/value cache based on the Least Recently Used algorithm

Downloads

68

Readme

LRU

NPM version GitHub last commit Github workflow Test coverage npm bundle size License

LRU is an in-memory key/value cache that relies on the Least Recently Used algorithm to maintain its size in a predefined limits.

LRU runs on both Node.js and ECMAScript 2015 (ES6) compliant browsers.

LRU is encapsulated in a module pattern. Only the variable LRU is accessible outside the module. Thus, it doesn't pollute the global space.

LRU is freely inspired from node-lru-cache.

Quick Startup

You can create your database object by typing:

const cache = LRU();

There is no need to use the new operator as LRU implements the prototypal instantiation pattern.

When, your cache is created, you can add a key/value by typing:

cache.set('a', { a: 1 });

if the key/value is already in the cache, the operation is ignored. But, you can force to update a key/value with the option force:

cache.set('a', { a: 2 }, { force: true });

You can read a key/value, from the cache, by typing:

cache.get('a');

API

The constructor

LRU accepts optionals parameters including: maxItems and maxAge.

By default, maxItems is set to 1000. It can't be lower than 1.

By default, maxAge is set to 1 hour. It can't be lower than 100ms.

To set different values, simply do:

const cache = LRU({ maxAge: 1000, maxItems: 100000 });

The methods

LRU provides the following methods:

  • set set(key, value) stores a new key/value into the cache and returns the added key/value.

    If the key key already exists into the cache, set returns the stored value.

    You can force to update the value by typing: set(key, newval, { force: true });

  • get get(key) returns the key/value stored into the cache. If this key/value doesn't exist, or it has reached its lifetime, get returns null.

  • has has(key) returns the key/value stored into the cache. If this key/value doesn't exist, or it has reached its lifetime, has returns null.

  • remove remove(key) returns the key/value stored into the cache and remove it from the cache. If this key/value doesn't exist, or it has reached its lifetime, remove returns null.

  • empty empty() removes all the key/values from the cache.

  • dump dump() returns an array with all the key(s)/value(s) stored into the cache that haven't exceeded their lifetime.

    The array looks like:

    [
      { key: 'a', value: 'aaa', age: 100 },
      { key: 'b', value: 'bbb', age: 80 },
      ...
    ]

    it is ordered from the oldest to the newest key/value.

  • prune prune() removes, from the cache, the key/value pairs that have exceeded their lifetime.

  • count count() returns the number of key(s)/value(s) stored into the cache.

  • renew renew(key) sets to zero the age of a key/value.

Remove programatically old key/value pairs

By default, a key/value pair that has exceeded its lifetime is removed from the cache only when a method get or has is performed.

If you want to automatically clean the cache, you have to pass a duration when you create the cache by typing:

const cache = LRU({ prune: 2000 });

The value is expressed in milliseconds. It can be lower that 1000. In the example above, the prune method is processed every 2000ms.

Embed LRU into your own library

You can easily embed LRU into your own library by a simple copy and paste. Or by using import.

Enjoy!

License

MIT.