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

simple-data-loader

v0.8.4

Published

Caches function results by key for specified time or indefintiy

Downloads

16

Readme

simple-data-loader

Introduction

simple-data-loader lets you cache a function's return value as a promise. It supports simple cache invalidation via timeouts in a minimal and dependency-free manner. It supports multi-argument functions and non-primitive arguments, and can be used for rate limiting.

simple-data-loader is heavily inspired by facebooks dataloader package and its api. However unlike facebooks dataloader, it places no emphasis on batching and as such allows for greater flexibility in selecting a loading function specific to your use-case.

It is designed to be simple, noise-free, have no external dependencies, and overall easy to use.

Installing

npm install simple-data-loader --save

Examples

caching resources

const dataloader = require('simple-data-loader');

function getResource(id) {
  // your implementation
}

const resouceLoader = dataloader(getResource, { ttl: 5000 });

const resourcePromise = resourceLoader('resourceID');

// await or the handle promise as you will.
// However resulted promise is cached for key `resourceID` for the next 5000 milliseconds

// If however you want to invalidate a key programitically you can:
resourceLoader.delete('resourceId');
resourceLoader('resourceId'); // will generate a new request for the underlying resource and cache it.

simple-data-loader can also be used to cache functions that have multiple arguments. This does not however mean that it can cache functions of variable arguments. It is only compatible with invocations with the same or less number of arguments as the function signature.

Setting a ttl (time to live) in the options works as in the previous example.

const dataloader = require('simple-data-loader');

function getBook(title, language, year) {
  // your implemenation
}

const bookLoader = dataloader(getBook);

const bookPromise1 = bookLoader('the old man and the sea', 'en', 1951);
const bookPromise2 = bookLoader('the old man and the sea', 'en', 1951);

bookPromise1 === bookPromise2; // true

bookLoader.delete('the old man and the sea', 'en', 1951);

const bookPromise3 = bookLoader('the old man and the sea', 'en', 1951);

bookPromise1 === bookPromise3; // false

Optionally one can choose to pass the loader function as part of the options object. The following code creates a loader with a timeout of 1 second.

const dataloader = require('simple-data-loader');

const myLoader = dataloader({
  load: () => {
    /*...*/
  },
  ttl: 1000,
});

By default simple-data-loader will not hash non-primitive types. This can be changed by setting the hash option to true. Fair warning the hash function is a simple deterministic version of JSON.stringify therefore caching using large objects or function definitions as arguments can be memory and computationally intensive. Use carefully.

For simple cases with functions that use option style objects this can be quite useful.

WARNING Circular objects will cause an infinite loop and eat all memory until the process crashes. Use hashing with care.

const dataloader = require('simple-data-loader');

function getListOfBooks(query, opts) {
  // your implementation
}

const loader = dataloader(getListOfBooks);

const promise1 = loader('hemingway', { limit: 5, sortBy: 'title' });
const promise2 = loader('hemingway', { limit: 5, sortBy: 'title' });

promise1 === promise2; // false, since although equal the two option objects are not the same reference

const hashLoader = dataloader(getListOfBooks, { hash: true });

const promise3 = loader('hemingway', { sortBy: 'name', limit: 5 });
const promise4 = loader('hemingway', { limit: 5, sortBy: 'name' });

promise3 === promise4; // true, the options objects needs only be equal. Key ordering does not matter

The max option can be passed to set a limit on the number of items that you want to cache. If a limit is set items will be evicted using the least recently used principle.

const dataloader = require('simple-data-loader');

const loader = dataloader(x => x, { max: 3 });
loader(1);
const promise2 = loader(2);
loader(1);
loader(3);
loader(4); // At this point "2" shall be evicted from the cache as it was the least recently used
const anotherPromise2 = loader(2);

promise2 === anotherPromise2; // false, this is not the same promise.

Supported options:

  • load (function => the loading function)
  • ttl (number => the time to live for cached items, if not provided will cache indefinitely unless programatically removed)
  • hash (boolean => enables hashing ie. a determinstic Stringify, and allows for non primitive type arguments)

Run the tests

from the package root run:

npm install
npm run test

License

This project is licensed under the MIT License - see the LICENSE.md file for details