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

stale-while-revalidate-lru-cache

v1.0.4

Published

Adds stale-while-revalidate scheme on top of lru-cache that will return dirty/stale values, while new ones are being updated

Downloads

117

Readme

stale-while-revalidate-lru-cache

An LRU cache with a maxAge and a staleWhileRevalidate, that will return dirty/stale values if the cached values are older than maxAge. If they are older than staleWhileRevalidate then new values will be fetched before returned.

Follows the stale-while-revalidate scheme implemented in browsers, https://web.dev/stale-while-revalidate/.

stale-while-revalidate-lru-cache sits on top of lru-cache. Adds a way to update/revalidate stale items (items that are older than maxAge), but continue to return the cached value.

Its usage is when you are using lru-cache asynchronously and are ok with returning dirty/stale/old values.

Note: Does not contain a "set" method, but all "sets" are being done through the validate method.

Installation

$ npm i stale-while-revalidate-lru-cache

Usage

Please read the Error handling for more production-ready example.

const SWRLRU = require('stale-while-revalidate-lru-cache')

// A cache that will contain some users.
// If the cache is older than a minute:
//   If it's less than an hour since last update/validation:
//     Return stale/dirty user, and `validate` in the background
//   If it's been more than an hour since last update/validation:
//     Wait for `validate` and then return
const users = SWRLRU({
  max: 30000, // Max 30,000 cached items
  maxAge: 60 * 1000, // one minute
  staleWhileRevalidate: 60 * 60 * 1000, // one hour
  validate: async params => {
    const username = params.username
    const token = params.token
    const user = await fetchUser({ username, token })

    return user
  }
})

const user = await users({
  key: 'MrFooBar',
  params: {
    username: 'MrFoobar'
  }
})
console.log(user) // This will not be printed until after validate/fetchUser is done feching the new user

setTimeout(async () => {
  const user = await users({
    key: 'MrFooBar',
    params: {
      username: 'MrFooBar',
      token: gotATokenFromSomewhere
    }
  })
  console.log(user) // This will be printed immediately, even though more than a minute has passed
}, 120 * 1000) // two minutes

Error handling

If there is an error thrown, or a promise rejected, in the validate method, then the lookup will throw an error. This needs to be handled, by try-catching:

const users = SWRLRU({
  max: 30000, // Max 30,000 cached items
  maxAge: 60 * 1000, // one minute
  staleWhileRevalidate: 60 * 60 * 1000, // one hour
  validate: async ({ username }) => {
    const user = await fetchUserFromDatabase({ username })
    return user
  }
})

try {
  const user = await users({
    key: 'a1b2c3',
    params: {
      username: 'MrFoobar'
    }
  })

  console.log(user)
} catch (err) {
  console.error('Could not get user from database', err)
}