GithubHelp home page GithubHelp logo

nickbalestra / async-memo-ize Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aboutlo/async-memo-ize

0.0 2.0 0.0 74 KB

Simple memoize utility ideal for functions with async/await syntax and promises. It supports cache in memory or via Redis

License: MIT License

JavaScript 96.25% Shell 3.75%

async-memo-ize's Introduction

Async Memoize

npm version CircleCI Greenkeeper badge

In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again โ€” Wikipedia

This library is an attempt to make async function, aka Promises, first class citizen with memoization

Use cases covered:

  1. An expensive function call (eg. API calls, intensive CPU calculations, etc)
  2. Multiple nodejs instances with a centralized cache (eg. Redis)

Notice: sync function can be used too

Real project use case

A docker cluster with multiple NodeJs nodes compute a calculation every day for each user The calculation is incremental using the data from the last 90 days. With this approach, the computation can be distributed across all the nodes available. It avoids crunching data from the previous days again and again.

Install

npm install async-memo-ize

or

yarn add  async-memo-ize

Usage

import memoize from 'async-memo-ize'
import sleep from 'sleep-promise';

const whatsTheAnswerToLifeTheUniverseAndEverything = async () => {
     await sleep(2000);
     return Promise.resolve(42)
}
const memoized = memoize(whatsTheAnswerToLifeTheUniverseAndEverything)

const answer = await memoized() // wait 2 seconds 
const quickAnswer = await memoized() // wait ms  

Cache

In Memory

A simple in memory async cache based on native js Map is provided.

Usage

import memoize, {SimpleCache} from 'async-memo-ize'

const fn = async () => Promise.resolve(42)
const memoized = memoize(fn, new SimpleCache)

const answer = await memoized() // wait ms  

You can provide your own implementation given the below interface:

class SimpleCache {

  async has(key) {
    ...
  }

  async get(key) {
    ...
  }

  async set(key, value) {
    ...
  }

  async del(key) {
    ...
  }

  async entries() {
    ...
  }

  async size() {
    ...
  }
}

Redis

If you want delegate and share the cache you can use RedisCache. The generated key is based on the function args and his name

Given:

const doSomething = async (a, b) => Promise.resolve(a+b)

The key generated:

["doSomething",1,5]

It means multiple nodejs instances can share the value computed if the function name and the args match.

Usage

import memoize, {RedisCache} from 'async-memo-ize'

const fn = async () => Promise.resolve(42)
const memoized = memoize(fn, new RedisCache())

const answer = await memoized() // wait ms  

test

prerequisites

docker run --name cache-redis -d -p 6379:6379 redis:alpine  

run

yarn test

async-memo-ize's People

Contributors

aboutlo avatar greenkeeper[bot] avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.