Giter Site home page Giter Site logo

feross / memo-async-lru Goto Github PK

View Code? Open in Web Editor NEW
18.0 3.0 1.0 12 KB

Memoize Node.js style callback-last functions, using an in-memory LRU store

License: MIT License

JavaScript 100.00%
memoize nodejs javascript browser browserify lru lru-cache callback

memo-async-lru's Introduction

memo-async-lru travis npm downloads javascript style guide

Memoize Node.js style callback-last functions, using an in-memory LRU store

Also works in the browser with browserify!

install

npm install memo-async-lru

usage

const memo = require('memo-async-lru')

function fn (arg, cb) {
  t.equal(arg, 'foo')
  cb(null, 'bar')
}

const memoFn = memo(fn)

memoFn('foo', (err, result) => {
  console.log(result) // prints 'bar'

  memoFn('foo', (err, result) => {
    console.log(result) // prints 'bar', cached, does not call fn()
  })
})

API

memo(fn, [opts])

Memoize the given function fn, using async-lru, a simple async LRU cache supporting O(1) set, get and eviction of old keys.

The function must be a Node.js style function, where the last argument is a callback.

function(key: Object, [...], fetch: function(err: Error, value: Object))

So, if you were to do:

const readFile = memo(fs.readFile)
readFile('file.txt', fn)
readFile('file.txt', fn) // <-- this uses the cache

The file would only be read from disk once, it's value cached, and returned anytime the first argument is 'file.txt'.

Repeated calls to the function with the same first argument will return a cached value, rather than re-fetch the data.

Optionally, an opts parameter can be specified with the following properties: Optional options:

{
  max: maxElementsToStore,
  maxAge: maxAgeInMilliseconds
}

If you pass max, items will be evicted if the cache is storing more than max items. If you pass maxAge, items will be evicted if they are older than maxAge when you access them.

license

MIT. Copyright (c) Feross Aboukhadijeh.

memo-async-lru's People

Contributors

diegorbaquero avatar feross avatar greenkeeper[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

diegorbaquero

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.