redis-streams

Extends the node redis client with readStream, writeStream, and writeThrough functions.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
redis-streams
1311.1.07 years ago8 years agoMinified + gzip package size for redis-streams in KB

Readme

redis-streams
!NPM Versionnpm-imagenpm-url !NPM Downloadsdownloads-imagedownloads-url !Build Statustravis-imagetravis-url !Test Coveragecoveralls-imagecoveralls-url
Extends the official noderedis 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.