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

invisible-cache

v3.0.0

Published

A caching system that lets you continue using your existing code without refactoring

Downloads

23

Readme

invisible-cache

A node.js caching system that lets you continue using your existing code without refactoring.

Install

  npm install invisible-cache

Introduction

This module lets you turn your existing data fetching functions into ones that have a TTL cache built in.

To alleviate refactoring work, the cached versions of the fetching functions have the same calling symatics as the original versions.

This module currently supports:

  • Promise based fetching functions
  • Callback style fetching functions

Within each category, there are three main calling symatics that are supported:

  • Fetching a single ID at a time.
  • Fetching multiple IDs where the function output is a hash whose keys are the input IDs to the function.
  • Fetching multiple IDs where the function output is an array of objects, each of which contains the corresponding input ID in an attribute.

If you have a fetching function that does not follow one of the above options, please let us know so that we can add support.

Note: As of v3, this package uses native promises, and not bluebird promises.

Usage

  const cachify = require('invisible-cache');
  const cachifyPromise = cachify.promise;
  const cachifyCallback = cachify.callback;

  const ttl = 60*1000; // one minute

  // fetching a single ID at a time - using promises
  function fetchUserPromise(id){ ... }
  const cachedFetchUserPromise =
    cachifyPromise.idToValue(ttl, fetchUserPromise);
  const userPromise = cachedFetchUserPromise(123);

  // fetching a single ID at a time - using callbacks
  function fetchUserCallback(id, cb){ ... }
  const cachedFetchUserCallback =
    cachifyCallback.idToValue(ttl, fetchUserCallback);
  cachedFetchUserCallback(123, (user) => { ... });

  // fetching many user rows at a time - using promises
  const attributeWithId = 'user_id';
  function fetchUsersPromise(ids){ ... }
  const cachedFetchUsersPromise =
    cachifyPromise.idsAsAttributes(ttl, fetchUsersPromise, attributeWithId);
  const usersPromise = cachedFetchUsersPromise([123,456]);

  // fetching many user rows at a time - using callbacks
  function fetchUsersCallback(ids, cb){ ... }
  const cachedFetchUsersCallback =
    cachifyCallback.idsAsAttributes(ttl, fetchUsersCallback, attributeWithId);
  cachedFetchUsersCallback([123,456], (users) => { ... }); 

  // fetching a hash with user_ids as the keys - using promises
  function fetchUsersHashPromise(ids){ ... }
  const cachedFetchUsersHashPromise =
    cachifyPromise.idsAsHashKeys(ttl, fetchUsersHashPromise);
  const usersHashPromise = cachedFetchUsersHashPromise([123,456]);

  // fetching a hash with user_ids as the keys - using callbacks
  function fetchUsersHashCallback(ids, cb){ ... }
  const cachedFetchUsersHashCallback =
    cachifyCallback.idsAsHashKeys(ttl, fetchUsersHashCallback);
  cachedFetchUsersHashCallback([123,456], (usersHash) => { ... });

Avoiding Refactoring

You can avoid refactoring by having the cached fetching function named as the original uncached fetching function.

  // old code
  function myFetchFunction(id){ ... }

  // new code
  function _myFetchFunction(id){ ... }
  const myFetchFunction =
    cachify.promise.idToValue(ttl, _myFetchFunction);