项目作者: styxlab

项目描述 :
Stream data from and to Redis with Node.
高级语言: TypeScript
项目地址: git://github.com/styxlab/redis-streams.git
创建时间: 2021-03-01T09:29:51Z
项目社区:https://github.com/styxlab/redis-streams

开源协议:

下载


redis-streams

Extends the redis client ioredis with streaming functions.
This library allows to read and write data into redis via node streams. The implementation is
inspired by redis-rstream and redis-wstream by @jeffbski and
has been simplified, made type safe with TypeScript and includes additional features.
This library can be conveniently used with async/await promise syntax.

The write stream has ben enhanced to include automatic generation of cryptographic hashes
such as sha1 and others. Furthermore the redis key can be automatically set to the
self-generated hash to allow for integrity checks and easy lookups.

If the maxBytes option is set on the write stream, an exception is thrown if the stream
exceeds that upper limit. You can also set the TTL option, in order to automatically remove
the data from the cache once the key is expired.

The main benefit of streaming is more efficient memory usage and safe-guards against buffer overflows.
Performance gains vary based on your hardware and depend on both data and chunk sizes. The default
chunk size has been set to 1 MB and can be changed through the options. Performance gains in my tests
were approximately 20% for writing into the redis cache and insignificant during reading from the cache.

Installation

  1. yarn add @jamify/redis-streams

Quick start

  1. import { StreamIORedis } from '@jamify/redis-streams'
  2. const redisClient = new StreamIORedis()
  3. redisClient.readStream(key)
  4. .pipe(createWriteStream('image.jpg'))
  5. .on('finish', done)
  6. createReadStream('image.jpg')
  7. .pipe(redisClient.writeStream(key))
  8. .on('finish', done)
  9. // promise version of writeStream
  10. await writeStreamPromise(createReadStream('image.jpg'), key)
  11. // Save to auto generated sha1 key
  12. await writeStreamPromise(createReadStream('image.jpg'), null, { algorithm: 'sha1' })
  13. // Throw exception, if maxBytes is exceeded
  14. await writeStreamPromise(createReadStream('image.jpg'), null, { algorithm: 'sha1', maxBytes: 10240 })

This will extend the IORedis client class with two additional functions:

readStream(key, options?): RedisRStream - Get a Readable stream from redis.

writeStream(key, options?): RedisWStream - Get a Writable stream from redis.

writeStreamPromise(rstream, key, options?): Promise<RedisWStream> - Promise version of writeStream(key)

Options for RedisRStream:

  1. {
  2. highWaterMark, // maximum internal buffer size
  3. }

Options for RedisWStream:

  1. {
  2. highWaterMark, // maximum internal buffer size
  3. clientMulti, // attach redis.multi in order to defer key rename
  4. algorithm, // crypto algorithm for hashing
  5. maxBytes, // maximum allowed bytes for writing
  6. ttl // key expire time in seconds
  7. }

Unit testing

  1. yarn test

Credits

Copyright & License

Copyright (c) 2020 Jamify - Released under the MIT license.