npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

thunk-redis

v2.2.4

Published

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

Downloads

767

Readme

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})
  • options.onlyMaster: Optional, Type: Boolean, Default: true.

    In replication mode, thunk-redis will try to connect master node and close slave node.

  • options.authPass: Optional, Type: String, Default: ''.

  • options.database: Optional, Type: Number, Default: 0.

  • options.returnBuffers: Optional, Type: Boolean, Default: false.

  • options.usePromise: Optional, Type: Boolean, Default: false.

    Export promise commands API.

    Use default Promise:

    var redis = require('thunk-redis')
    var client = redis.createClient({
      usePromise: true
    })
  • options.noDelay: Optional, Type: Boolean, Default: true.

    Disables the Nagle algorithm. By default TCP connections use the Nagle algorithm, they buffer data before sending it off. Setting true for noDelay will immediately fire off data each time socket.write() is called.

  • options.retryMaxDelay: Optional, Type: Number, Default: 5 * 60 * 1000.

    By default every time the client tries to connect and fails time before reconnection (delay) almost multiply by 1.2. This delay normally grows infinitely, but setting retryMaxDelay limits delay to maximum value, provided in milliseconds.

  • options.maxAttempts: Optional, Type: Number, Default: 20.

    By default client will try reconnecting until connected. Setting maxAttempts limits total amount of reconnects.

  • options.pingInterval: Optional, Type: Number, Default: 0.

    How many ms before sending a ping packet. There is no ping packet by default(0 to disable). If redis server enable timeout config, this option will be useful.

  • options.IPMap: Optional, Type: Object, Default: {}.

    This option use o resolve redis internal IP and external IP problem https://github.com/thunks/thunk-redis/issues/19. Define it like: {internalIP: externalIP}, for example:

    const cli = redis.createClient([
      '127.0.0.1:7000',
      '127.0.0.1:7001',
      '127.0.0.1:7002',
      '127.0.0.1:7003',
      '127.0.0.1:7004',
      '127.0.0.1:7005'
    ], {
      IPMap: {
        '172.17.0.2:7000': '127.0.0.1:7000',
        '172.17.0.2:7001': '127.0.0.1:7001',
        '172.17.0.2:7002': '127.0.0.1:7002',
        '172.17.0.2:7003': '127.0.0.1:7003',
        '172.17.0.2:7004': '127.0.0.1:7004',
        '172.17.0.2:7005': '127.0.0.1:7005'
      }
    })