Home

Awesome

thunk-redis

The fastest thunk/promise-based redis client, support all redis features.

NPM version Build Status Downloads js-standard-style

thunks

Implementations:

Demo(examples)

https://raw.githubusercontent.com/antirez/redis/4.0/redis.conf

Sugest set config cluster-require-full-coverage to no in redis cluster!

default thunk API:

const redis = require('../index')
const thunk = require('thunks')()
const client = redis.createClient({
  database: 1
})

client.on('connect', function () {
  console.log('redis connected!')
})

client.info('server')(function (error, res) {
  console.log('redis server info:', res)
  return this.dbsize()
})(function (error, res) {
  console.log('current database size:', res)
  // current database size: 0
  return this.select(0)
})(function (error, res) {
  console.log('select database 0:', res)
  // select database 0: OK
  return thunk.all([
    this.multi(),
    this.set('key', 'redis'),
    this.get('key'),
    this.exec()
  ])
})(function (error, res) {
  console.log('transactions:', res)
  // transactions: [ 'OK', 'QUEUED', 'QUEUED', [ 'OK', 'redis' ] ]
  return this.quit()
})(function (error, res) {
  console.log('redis client quit:', res)
  // redis client quit: OK
})

use promise API:

const redis = require('../index')
const Promise = require('bluebird')
const client = redis.createClient({
  database: 1,
  usePromise: true
})

client.on('connect', function () {
  console.log('redis connected!')
})

client
  .info('server')
  .then(function (res) {
    console.log('redis server info:', res)
    return client.dbsize()
  })
  .then(function (res) {
    console.log('current database size:', res)
    // current database size: 0
    return client.select(0)
  })
  .then(function (res) {
    console.log('select database 0:', res)
    // select database 0: OK
    return Promise.all([
      client.multi(),
      client.set('key', 'redis'),
      client.get('key'),
      client.exec()
    ])
  })
  .then(function (res) {
    console.log('transactions:', res)
    // transactions: [ 'OK', 'QUEUED', 'QUEUED', [ 'OK', 'redis' ] ]
    return client.quit()
  })
  .then(function (res) {
    console.log('redis client quit:', res)
    // redis client quit: OK
  })
  .catch(function (err) {
    console.error(err)
  })

support generator in thunk API:

const redis = require('thunk-redis')
const client = redis.createClient()

client.select(1)(function* (error, res) {
  console.log(error, res)

  yield this.set('foo', 'bar')
  yield this.set('bar', 'baz')

  console.log('foo -> %s', yield this.get('foo'))
  console.log('bar -> %s', yield this.get('bar'))

  var user = {
    id: 'u001',
    name: 'jay',
    age: 24
  }
  // transaction, it is different from node_redis!
  yield [
    this.multi(),
    this.set(user.id, JSON.stringify(user)),
    this.zadd('userAge', user.age, user.id),
    this.pfadd('ageLog', user.age),
    this.exec()
  ]

  return this.quit()
})(function (error, res) {
  console.log(error, res)
})

Benchmark

Details: https://github.com/thunks/thunk-redis/issues/12

Installation

Node.js:

npm install thunk-redis

API (More)

  1. redis.createClient([addressArray], [options])
  2. redis.createClient([port], [host], [options])
  3. redis.createClient([address], [options])
  4. redis.calcSlot(str)
  5. redis.log([...])

redis.log

Helper tool, print result or error stack.

const client = redis.createClient()
client.info()(redis.log)

redis.createClient

const client1 = redis.createClient()
const client2 = redis.createClient({database: 2})
const client3 = redis.createClient(6379, {database: 2})
const client4 = redis.createClient('127.0.0.1:6379', {database: 2})
const client5 = redis.createClient(6379, '127.0.0.1', {database: 2})
// connect to 2 nodes
const client6 = redis.createClient([6379, 6380])
const client7 = redis.createClient(['127.0.0.1:6379', '127.0.0.1:6380']) // IPv4
const client8 = redis.createClient(['[::1]:6379', '[::1]:6380']) // IPv6

redis cluster:

// assume cluster: '127.0.0.1:7000', '127.0.0.1:7001', '127.0.0.1:7002', ...
const client1 = redis.createClient(7000, options) // will auto find cluster nodes!
const client2 = redis.createClient([7000, 7001, 7002], options)

const client3 = redis.createClient([
  '127.0.0.1:7000',
  '127.0.0.1:7001',
  '127.0.0.1:7002'
], options)

const client4 = redis.createClient([
  {host: '127.0.0.1', port: 7000},
  {host: '127.0.0.1', port: 7001},
  {host: '127.0.0.1', port: 7002},
], options)
// All of above will work, it will find redis nodes by self.

// Create a client in cluster servers without cluster mode:
const clientX = redis.createClient(7000, {clusterMode: false})