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

@s-ui/redis-lru

v1.0.0

Published

redis-backed LRU cache

Downloads

24

Readme

redis lru cache Build Status

A least recently used (LRU) cache backed by Redis, allowing data to be shared by multiple Node.JS processes. API inspired by node-lru-cache.

var redis = require('redis').createClient(port, host, opts);
var lru = require('redis-lru');

var personCache = lru(redis, 5); // up to 5 items

personCache.set('john', {name: 'John Doe', age: 27})
  .then(() => personCache.set('jane', {name: 'Jane Doe', age: 30}))
  .then(() => personCache.get('john'))
  .then(console.log) // prints {name: 'John Doe', age: 27}
  .then(() => personCache.reset()) //clear the cache

var bandCache = lru(redis, {max: 2, namespace: 'bands', maxAge: 15000}); // use a different namespace and set expiration

bandCache.set('beatles', 'john, paul, george, ringo')
  .then(() => bandCache.set('zeppelin', 'jimmy, robert, john, bonzo'))
  .then(() => bandCache.get('beatles')) // now beatles are the most recently accessed
  .then(console.log) // 'john, paul, george, ringo'
  .then(() => bandCache.set('floyd', 'david, roger, syd, richard, nick')) // cache full, remove least recently accessed
  .then(() => bandCache.get('zeppelin'))
  .then(console.log) // null, was evicted from cache

Works both with node_redis and ioredis clients.

Installation

npm install redis-lru

Options

  • max: Maximum amount of items the cache can hold. This option is required; if no other option is needed, it can be passed directly as the second parameter when creating the cache.
  • namespace: Prefix appended to all keys saved in Redis, to avoid clashes with other applications and to allow multiple instances of the cache.
  • maxAge: Maximum amount of milliseconds the key will be kept in the cache; after that getting/peeking will resolve to null. Note that the value will be removed from Redis after maxAge, but the key will be kept in the cache until next time it's accessed (i.e. it will be included in count, keys, etc., although not in has.).
  • score: function to customize the score used to order the elements in the cache. Defaults to () => new Date().getTime()
  • increment: if true, on each access the result of the score function is added to the previous one, rather than replacing it.

API

All methods return a Promise.

  • set(key, value, maxAge): set value for the given key, marking it as the most recently accessed one. Keys should be strings, values will be JSON.stringified. The optional maxAge overrides for this specific key the global expiration of the cache.
  • get(key): resolve to the value stored in the cache for the given key or null if not present. If present, the key will be marked as the most recently accessed one.
  • getOrSet(key, fn, maxAge): resolve to the value stored in the cache for the given key. If not present, execute fn, save the result in the cache and return it. fn should be a no args function that returns a value or a promise. If maxAge is passed, it will be used only if the key is not already in the cache.
  • peek(key): resolve to the value stored in the cache for the given key, without changing its last accessed time.
  • del(key): removes the item from the cache.
  • reset(): empties the cache.
  • has(key): resolves to true if the given key is present in the cache.
  • keys(): resolves to an array of keys in the cache, sorted from most to least recently accessed.
  • values(): resolves to an array of values in the cache, sorted from most to least recently accessed.
  • count(): resolves to the number of items currently in the cache.

Using as a LFU cache

By using a custom score function and the increment option, one can turn the cache into a least frequently used (LFU), where the items that have been accessed more times (rather than most recently) are preserved:

var redis = require('redis').createClient(port, host, opts);
var lru = require('redis-lru');

var bandLfu = lru(redis, {max: 2, score: () => 1, increment: true});

bandLfu.set('beatles', 'john, paul, george, ringo')
  .then(() => bandLfu.get('beatles')) // accessed twice
  .then(() => bandLfu.set('zeppelin', 'jimmy, robert, john, bonzo'))
  .then(() => bandLfu.set('floyd', 'david, roger, syd, richard, nick')) // cache full, remove least frequently accessed
  .then(() => bandLfu.get('zeppelin'))
  .then(console.log) // null, was evicted from cache

Implementation

Each item in the cache is stored as a regular key/value in Redis. Additionally, a ZSET is used to keep an index of the keys sorted by last-accessed timestamp.

Requires Redis 3.0.2 or greater, since it uses the XX option of ZADD.