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

async-cacheify

v1.4.0

Published

Wrapper for async functions

Downloads

3

Readme

async-cacheify

Wrapper for async functions.

Usage

We can expect expensiveFunction is something we don't want to run very often. So we wrap it with this library, for the lifetime of the application it will only run the first time.

const cacheify = require('async-cacheify');

async function expensiveFunction () {
    return await expensiveThing('x', 'y', 'z');
}

const cheapFunction = cacheify(expensiveFunction);

const results = await Promise.all([
    cheapFunction(),
    cheapFunction(),
    cheapFunction(),
    cheapFunction(),
    cheapFunction(),
    cheapFunction(),
    cheapFunction()
]);

We are able to run cheapFunction a lot for free.

Parameters

You can use parameters as you would expect.

Any parameters you pass create a new cache only relevant for when those specific parameters are used again. In the following example the function will run twice, one time with 'x', 'y' and then with 'q', 'y'.

const cacheify = require('async-cacheify');

async function expensiveFunction (x, y) {
    return await expensiveThing(x, y, 'z');
}

const cheapFunction = cacheify(expensiveFunction);

const result1 = await cheapFunction('x', 'y');
const result2 = await cheapFunction('q', 'y');
const result3 = await cheapFunction('q', 'y');
const result4 = await cheapFunction('x', 'y');

Expiry

It is possible to set a cache expiry so that the function will execute again whenever the data gets old. The following example demonstrates a 1000ms cooldown.

const cacheify = require('async-cacheify');

async function expensiveFunction (x, y) {
    return await expensiveThing(x, y, 'z');
}

const cheapFunction = cacheify(expensiveFunction, 1000);

Parallel function calls

By setting the cache expiry to 0 you will not have the benefit of a long running cache. However if the function is run more than once before the first invocation finishes they will still share the result without it running multiple times.

const cacheify = require('async-cacheify');

async function expensiveFunction (x, y) {
    return await expensiveThing(x, y, 'z');
}

const cheapFunction = cacheify(expensiveFunction, 0);

const [result1, result2] = await Promise.all([
    cheapFunction('x', 'y'),
    cheapFunction('x', 'y')
]);

Flush

It is possible to force the function to run by using flush.

The example below flushes the cache twice and runs the function three times. If there is already an invocation running that invocation will finish in the background and complete as normal but new invocations will wait for the new one.

const cacheify = require('async-cacheify');

async function expensiveFunction (x, y) {
    return await expensiveThing(x, y, 'z');
}

const cheapFunction = cacheify(expensiveFunction);

const result1 = await cheapFunction('x', 'y');
cheapFunction.flush('x', 'y');
const result2 = await cheapFunction('x', 'y');
const result3 = await cheapFunction('x', 'y');
cheapFunction.flush('x', 'y')
const result4 = await cheapFunction('x', 'y');

Flush all

You can clear the cache for the entire function and not just specific parameters by using flushAll.

const cacheify = require('async-cacheify');

async function expensiveFunction (x, y) {
    return await expensiveThing(x, y, 'z');
}

const cheapFunction = cacheify(expensiveFunction);

const result1 = await cheapFunction('x', 'y');
const result2 = await cheapFunction('x', 'y');
cheapFunction.flushAll();
const result3 = await cheapFunction('x', 'y');
const result4 = await cheapFunction('q', 'y');
cheapFunction.flushAll();

Errors

Any error thrown does not store a cache. Therefore further invocations will keep trying to get it to work until there is a cache again.

const cacheify = require('async-cacheify');

async function expensiveFunction () {
    throw new Error('Kabooooooom!');
}

const cheapFunction = cacheify(expensiveFunction);

try {
    await Promise.all([cheapFunction(), cheapFunction()]);
} catch (e) {
    console.log(e.message); // Kabooooooom!
}
try {
    await cheapFunction();
} catch (e) {
    console.log(e.message); // Kabooooooom!
}

Contribute

Sure!