redis-streams

1.1.0 • Public • Published

redis-streams

NPM Version NPM Downloads Build Status Test Coverage

Extends the official node_redis client with additional functionality to support streaming data into and out of Redis avoiding buffering the entire contents in memory. The real work is powered by the redis-rstream and redis-wstream by @jeffbski.

Installation

npm install redis-streams

Usage

var redis = require('redis');
require('redis-streams')(redis);

This will extend the RedisClient prototype with two additional functions:

readStream(key) - get a Readable stream from redis.

writeStream(key, maxAge) - get a Writable stream from redis.

writeThrough(key, maxAge) - write to redis and pass the stream through.

var redis = require('redis');
require('redis-streams')(redis);
 
var redisClient = redis.createClient();
 
redisClient.readStream(key)
    .pipe(process.stdout);
 
fs.createReadStream('file.txt')
    .pipe(redisClient.writeStream(key, maxAge))
    .on('finish', done);
 
fs.createReadStream('file.txt')
    .pipe(redisClient.writeThrough(key, maxAge))
    .pipe(process.stdout);

See the unit tests for additional usage examples.

Caching Proxy

You could also implement a Connect caching proxy middleware.

var redis = require('redis');
var request = require('request');
require('redis-streams')(redis);
 
var redisClient = redis.createClient();
 
app.get('/cache/:key', function(req, res, next) {
    redis.exists(req.params.key, function(err, exists) {
       if (err) return next(err);
 
        if (exists)
            return redis.readStream(req.params.key).pipe(res);
 
        // Cache the remote http call for 60 seconds
        request.get('http://somewhere.com/' + req.params.key)
            .pipe(redis.writeThrough(req.params.key, 60))
            .pipe(res);
    });
});

The express-api-proxy module utilizes redis-streams for this purpose, but in a more advanced way.

Dependencies (4)

Dev Dependencies (4)

Package Sidebar

Install

npm i redis-streams

Weekly Downloads

629

Version

1.1.0

License

MIT

Last publish

Collaborators

  • aerobatic
  • dvonlehman