3.0.0 • Published 5 years ago

cached-callback v3.0.0

Weekly downloads
31
License
ISC
Repository
github
Last release
5 years ago

cached-callback

Greenkeeper badge

cached-callback caches arguments returned from an earlier execution and passes them to a callback passed in

Examples

var cachedCallback = require('cached-callback')

Debounce

Debounces an invocation with a specific key as long as it's callback is not yet called

var debounced = cachedCallback(function (id, callback) {
  request('http://example.com/'+id, callback)
})

// The request only gets triggered once even when you
// invoke the wrapped method multiple times.
debounced('test.html', function (err, data) { console.log(err, data) })
debounced('test.html', function (err, data) { console.log(err, data) })

// So this shouldn't result in the same output
setTimeout(function () {
  debounced('test.html', function (err, data) { console.log(err, data) })
}, 10000)

Persistent cache

Permanently caches the result of the callback. Multiple invocations result in the same output. (Only use this in a controlled environment. This fills an object with the whole response of the callback. So it might lead to a memory leak.)

var cached = cachedCallback(function (id, callback) {
  request('http://example.com/'+id, callback)
}, true)

cached('test.html', function (err, data) { console.log(err, data) })

// This is definitely the same response
setTimeout(function () {
  cached('test.html', function (err, data) { console.log(err, data) })
}, 10000)

The second cache argument is also exposed with .cache()

var cached = require('cached-callback').cache()
var get = cached(function (id, callback) {
  request('http://example.com/'+id, callback)
}

Custom caching method

Caches the result with a custom caching method. This example caches the result for 20 seconds.

var cache = {}
var setterAndGetter = {
  get: function get (key) {
    return cache[key]
  },
  set: function set (key, value) {
    cache[key] = value
    setTimeout(function () { delete cache[key] }, 20000)
  }
}

var custom = cachedCallback(function (id, callback) {
  request('http://example.com/'+id, callback)
}, setterAndGetter)

Why don't I use async.memoize?

I ended up writing similar code like this module tons of times and didn't find async.memoize when I needed it. IMO asyncjs also got too large and could benefit from some modularization.

The advantage of this module is that you can hook up a custom cache method. E.g. a lru cache like https://www.npmjs.com/package/lru-cache

3.0.0

5 years ago

1.0.0

5 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.0

8 years ago