/redigo-pipeline-pool

Simple Go Redis pipeline pool client base on Redigo

Primary LanguageGo

Redigo-Pipeline-Pool

Simple Go Redis pipeline pool client based on Redigo.

This is a connection free Redis pool. It handles all operations of connections by itself. You don't need to deal with Redis connections.

This pool uses pipeline to execute Redis commands. Multiple commands can execute on one connection at the same time. So you don't need too many connections.

You can use delay and maxPendingSize to compress multiple commands into fewer TCP packages. This can reduce network traffic.

Why pool?

You can use only one connection with pipeline to do the same things. But multiple connections can help you use multiple cores of CPU and multi-queue of network interface controller to improve performance.

Installation

Install Redigo-Pipeline-Pool using the "go get" command:

go get github.com/holdstop/redigo-pipeline-pool/pool

Usage

Import Redigo-Pipeline-Pool and Redigo.

import (
	"github.com/holdstop/redigo-pipeline-pool/pool"
	"github.com/gomodule/redigo/redis"
)

Use the pool.NewPool to create a new pool.

func NewPool(dial func() (redis.Conn, error), connSize int, reconnectInterval time.Duration, delay time.Duration, maxPendingSize int, maxWaitingSize int) (Pool, error)
p, err := pool.NewPool(
	func() (redis.Conn, error) {
		return redis.Dial("tcp", "127.0.0.1:6379")
	}, // Function returns a Redis connection.
	5, // Number of Redis connections in the pool.
	1 * time.Second, // Reconnect interval when connection encountered unrecoverable error.
	100 * time.Microsecond, // The longest delay time waiting for the number of buffered requests to be maxPendingSize before flush to the Redis server.
	50, // Maximum number of requests buffered before flush to the Redis server. (per connection)
	500, //Maximum number of unreturned requests waited before send new requests. (per connection)
)

Use Do and DoScript to execute Redis commands and scripts.

Do(cmd string, args ...interface{}) (interface{}, error)
DoScript(script *redis.Script, keysAndArgs ...interface{}) (interface{}, error)
reply, err := redis.String(p.Do("SET", "foo", "bar"))
fmt.Println(reply, err)
reply, err = redis.String(p.Do("GET", "foo"))
fmt.Println(reply, err)

script := redis.NewScript(1, "return redis.call(\"GET\", KEYS[1])")
reply, err = redis.String(p.DoScript(script, "foo"))
fmt.Println(reply, err)

Use Multi and DoMulti to execute Redis transactions.

type Multi struct {
}
func (multi *Multi) Do(cmd string, args ...interface{})
DoScript(script *redis.Script, keysAndArgs ...interface{}) (interface{}, error)
multi := pool.Multi{}
multi.Do("SET", "foo", "bar")
multi.Do("GET", "foo")
fmt.Println(redis.Strings(p.DoMulti(multi)))

Use Close to close the pool

p.Close()

Supported commands

Supports command groups Geo Hashes Keys HyperLogLog Lists Sets Sorted Sets Strings on https://redis.io/commands, without commands WAIT MIGRATE in group Keys.