a through stream that caches

npm install through-cache
10 downloads in the last month


A through stream that caches


There are some cases where the same data is piped over and over again. In these cases there is no reason to repeat the transformation of the data when the result can be retrieved from a cache.


Create a cache and pipe to its cache method.

// create a new cache
var tc = require('through-cache')()

a.pipe(tc.cache(function(obj) {

    // this only gets called once because
    // the exact same data is written again.
    // but two writes are made to the `b` stream,
    // one from the `a` stream and one from the 
    // cache.

    var str = '<div>' + obj.value + '</div>'


stream.write({ value: 'hello, world' })
stream.write({ value: 'hello, world' })

To cache a function that will get applied to the data.

a.pipe(tc.cache(function(obj) {

    // the transform function will get cached and then 
    // applied to the data every time data gets pushed 
    // to the stream.

    this.transform(function(data) {
        return '<div>' + data + '</div>'



And to invalidate the cache.

npm loves you