memoize-async

3.0.3 • Public • Published

memoize-async

Async memoization

Code Climate Test Coverage Issue Count Circle CI

npm install memoize-async

API

memoize(fn: function [, keyGen: function]): Promise

  • fn: function to evaluate (can be async or sync)
  • keyGen: function used to generate a key from fn params, useful for complex memoization (can be async or sync)

memoize-async expects the global Promise to be defined


Examples

Standard:

import memoize from 'memoize-async'
import { get } from 'highwire'
import { retry } from 'u-promised'
 
const fn = (url) =>
  retry(3, () =>
    get(url).then((response) =>
      JSON.parse(response.body)))
 
const memFetch = memoize(fn)
 
memFetch('http://some.api/things')
  .then((things) => console.log(things)) // must resolve from fn
  .then(() => memFetch('http://some.api/things'))
  .then((things) => console.log(things)) // resolves from cache
 

With KeyGen:

import memoize from 'memoize-async'
import { get } from 'highwire'
import { retry } from 'u-promised'
 
const fn = (user) =>
  retry(3, () =>
    get(user.profile).then((response) =>
      JSON.parse(response.body)))
 
const keyGen = (user) => user.id
 
const memFetchUserProfile = memoize(fn, keyGen)
 
const user = {
  id: '1234-5678'
  profile: 'http://some.api/users/1234-5678'
}
 
memFetchUserProfile(user)
  .then((profile) => console.log(profile)) // sets cache key to '1234-5678'
  .then(() => memFetchUserProfile(user))
  .then((profile) => console.log(profile)) // resolves from cache
 

Clearing cache:

import memoize from 'memoize-async'
import { get } from 'highwire'
import { retry } from 'u-promised'
 
const url = 'http://some.api/things'
 
const fn = (url) =>
  retry(3, () =>
    get(url).then((response) =>
      JSON.parse(response.body)))
 
const memFetch = memoize(fn)
 
const CLEAR_TIMER = 1000 * 60 * 5
 
memFetch(url)
  .then((things) => console.log(things))
  .then(() => setTimeout(() => memFetch.cache.delete(url), CLEAR_TIMER))
  // also
  .then(() => setTimeout(() => memFetch.cache.clear(), CLEAR_TIMER))
 

Testing

Run npm test. To also generate coverage run npm run test:coverage.

Readme

Keywords

Package Sidebar

Install

npm i memoize-async

Weekly Downloads

16

Version

3.0.3

License

MIT

Last publish

Collaborators

  • kkemple