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

@andrewshell/cacheism

v2.0.0

Published

Simple caching library

Downloads

3

Readme

cacheism

Simple caching library

Node.js CI

Overview

The goal of cacheism is to wrap an async function with caching logic where we can easily specify under what circumstances we want to return the cache or fetch the live data.

Your callback will get passed to it a Hit if there is an existing cache stored or a Miss if there is no existing cache.

const Cacheism = require('@andrewshell/cacheism');

const datadir = __dirname + '/data';
const cache = new Cacheism(Cacheism.store.filesystem({ datadir }));

async function run() {
    let result = await cache.go('-internal', 'hoopla', Cacheism.Status.cacheOnFail, async (existing) => {
        if (Math.random() < 0.5) {
            throw Error('Death');
        }
        return { message: 'Hoopla!' };
    });

    if (result.isHit) {
        console.dir(result.data);
    }

    if (result.error) {
        console.error(result.error);
    }
}

run().catch(err => console.error(err));

Statuses

Only Fresh

The onlyFresh status is for times where we never want to use the cache, but we want to fetch the fresh data and store it in the cache for other requests.

Cache on Fail

The cacheOnFail status is for times where we want to try to fetch fresh data, but if an error is thrown, use the cache if present.

Prefer Cache

The preferCache status is for times where we want to use the cache if available and only fetch fresh if the cache is not available.

Only Cache

The onlyCache status if for times where we don't want to attempt to fetch fresh data and only return the cache if present.

Results

The cache.go function will always return either a Hit or a Miss object.

Hit

A hit is returned when we have good data. The cached param will be true if the data was fetched from cache versus fresh data.

Hit {
  version: 3,
  cacheName: '-internal/hoopla',
  cached: true,
  created: 2023-04-02T22:00:49.320Z,
  data: { message: 'Hoopla!' },
  error: Error: Death
      at /Users/andrewshell/code/test-cacheism/index.js:9:19
      at Cacheism.go (/Users/andrewshell/code/cacheism/lib/cacheism.js:30:30)
      at async run (/Users/andrewshell/code/test-cacheism/index.js:7:18),
  errorTime: 2023-04-02T22:00:49.928Z,
  consecutiveErrors: 1,
  etag: '"15-QcHvuZdyxCmLJ4zoYIPsP6pkNoM"',
  isHit: true,
  isMiss: false
}

In the case of Cache on Fail, the error param may be set which is the error thrown while fetching fresh data.

Miss

A miss is returned when we don't have good data. For instance, if there wasn't cached data and an error was thrown while fetching fresh data. You'll also get a miss if you fetch with the onlyCache status and there isn't a cache.

Miss {
  version: 3,
  cacheName: '-internal/hoopla',
  cached: false,
  created: 2023-04-02T22:02:30.294Z,
  data: null,
  error: Error: Missing cache
      at Cacheism.go (/Users/andrewshell/code/cacheism/lib/cacheism.js:28:19)
      at async run (/Users/andrewshell/code/test-cacheism/index.js:7:18),
  errorTime: 2023-04-02T22:02:30.294Z,
  consecutiveErrors: 1,
  etag: null,
  isHit: false,
  isMiss: true
}