Skip to content

Repository files navigation

redisgo

GoDoc

A high performance and simple redis client for Go(golang).

It is inspired by redigo.

here is benchmark results compare to redigo and go-redis with go1.10.1, i7-7700:

BenchmarkCmdSetRedisgo 	10000000	       188 ns/op	       5 B/op	       0 allocs/op
BenchmarkCmdSetRedigo  	 5000000	       329 ns/op	     112 B/op	       5 allocs/op
BenchmarkCmdSetGoredis 	 2000000	       748 ns/op	     372 B/op	       9 allocs/op

Usage

package main

import (
    "context"
    "log"
    "net"

    "github.com/xiaost/redisgo"
)

func main() {
    pool := redisgo.NewPool(func(ctx context.Context) (*redisgo.Conn, error) {
        conn, err := net.Dial("tcp", "127.0.0.1:6379")
        if err != nil {
            return nil, err
        }
        return redisgo.NewConn(conn), nil
    })

    redisconn, err := pool.Get(context.TODO())
    if err != nil {
        log.Fatal(err)
    }
    defer redisconn.Close() // returns to pool

    err = redisconn.DoNoReply("SET", "hello", "world")
    /*  or you can do it like this:
    reply, err := c.Do(cmd, args...)
    if err == nil {
        err = reply.Err()
        reply.Free()
    }
    */
    if err != nil {
        log.Fatal(err)
    }

    b, err := redisconn.DoBytes("GET", "hello")
    /* or you can do it like this:
    var b []byte
    reply, err := c.Do("GET", "hello")
    if err == nil {
        b, err = reply.Bytes()
        reply.Free()
    }
    */
    if err != nil { // err == redisgo.ErrNil if not exists
        log.Fatal(err)
    }
    if s := string(b); s != "world" {
        log.Fatal(s)
    }
}

Notes

  • Conn is not safe for concurrent use; borrow one Conn per goroutine from a Pool.
  • Replies are zero-copy: byte slices returned by Reply.Bytes, DoBytes, DoBytesSlice and RedisErr alias the read buffer of the connection. They stay valid after Reply.Free, but keeping one alive keeps the whole buffer alive, so copy values that you retain for long.
  • Call Reply.Free exactly once per Reply returned by Do.
  • Bulk strings and arrays longer than 512MB (the default proto-max-bulk-len of redis) are rejected as a protocol error.

About

A high performance and simple redis client for Go(golang)

Topics

Resources

Stars

9 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages