Type-safe Redis client for Golang
Go to file
Vladimir Mihailenco ab4d0d6b62 Change redis.Nil value. 2013-11-04 10:08:03 +02:00
v2 Change redis.Nil value. 2013-11-04 10:08:03 +02:00
LICENSE Add license. Fixes #1. 2012-12-19 13:05:50 +02:00
README.md readme: Mention new version. 2013-10-15 13:51:40 +03:00
commands.go Fix ZRevRangeByScoreWithScores to return scores. 2013-06-25 11:11:07 +03:00
connpool.go Use net.Conn instead of io.ReadWriteCloser. 2012-08-20 14:50:25 +03:00
doc.go Update doc. 2013-02-17 20:43:14 +02:00
example_test.go Fix all examples in readme. Req now implements Stringer interface for debugging purposes. 2013-02-17 18:49:17 +02:00
multi.go multi: Restrict Discard usage. 2012-08-25 22:51:42 +03:00
parser.go Add test for reading many keys. 2013-03-21 12:28:19 +02:00
parser_test.go Add ParseReq method and tweak benchmarks. 2012-08-24 15:16:12 +03:00
pipeline.go redis: Rename mtx to reqsMtx. 2012-08-25 22:44:53 +03:00
pubsub.go pub/sub: Clean up message parsing. 2012-08-25 21:39:35 +03:00
redis.go Remove unused variables. 2013-02-02 12:58:31 +02:00
redis_test.go Fix ZRevRangeByScoreWithScores to return scores. 2013-06-25 11:11:07 +03:00
req.go Fix all examples in readme. Req now implements Stringer interface for debugging purposes. 2013-02-17 18:49:17 +02:00
req_test.go Add ParseReq method and tweak benchmarks. 2012-08-24 15:16:12 +03:00
script.go Add Lua script helper. 2013-05-22 17:56:59 +03:00

README.md

New version

Make sure to check new version of the client that got better API and timeout support: https://github.com/vmihailenco/redis/tree/master/v2

Redis client for Golang

Supports:

  • Redis 2.6 commands except QUIT, MONITOR, SLOWLOG and SYNC.
  • Pub/sub.
  • Transactions.
  • Pipelining.
  • Connection pool.
  • TLS connections.
  • Thread safety.

API docs: http://godoc.org/github.com/vmihailenco/redis

Installation

Install:

go get github.com/vmihailenco/redis

Getting started

Let's start with connecting to Redis using TCP:

password := ""  // no password set
db := int64(-1) // use default DB
client := redis.NewTCPClient("localhost:6379", password, db)
defer client.Close()

ping := client.Ping()
fmt.Println(ping.Err(), ping.Val())
// Output: <nil> PONG

or using Unix socket:

client := redis.NewUnixClient("/tmp/redis.sock", "", -1)
defer client.Close()

ping := client.Ping()
fmt.Println(ping.Err(), ping.Val())
// Output: <nil> PONG

Then we can start sending commands:

set := client.Set("foo", "bar")
fmt.Println(set.Err(), set.Val())

get := client.Get("foo")
fmt.Println(get.Err(), get.Val())

// Output: <nil> OK
// <nil> bar

We can also pipeline two commands together:

var set *redis.StatusReq
var get *redis.StringReq
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
    set = c.Set("key1", "hello1")
    get = c.Get("key2")
})
fmt.Println(err, reqs)
fmt.Println(set)
fmt.Println(get)
// Output: <nil> [SET key1 hello1: OK GET key2: (nil)]
// SET key1 hello1: OK
// GET key2: (nil)

or:

var set *redis.StatusReq
var get *redis.StringReq
reqs, err := client.Pipelined(func(c *redis.PipelineClient) {
    set = c.Set("key1", "hello1")
    get = c.Get("key2")
})
fmt.Println(err, reqs)
fmt.Println(set)
fmt.Println(get)
// Output: <nil> [SET key1 hello1 GET key2]
// SET key1 hello1
// GET key2

We can also send several commands in transaction:

func transaction(multi *redis.MultiClient) ([]redis.Req, error) {
    get := multi.Get("key")
    if err := get.Err(); err != nil && err != redis.Nil {
        return nil, err
    }

    val, _ := strconv.ParseInt(get.Val(), 10, 64)

    reqs, err := multi.Exec(func() {
        multi.Set("key", strconv.FormatInt(val+1, 10))
    })
    // Transaction failed. Repeat.
    if err == redis.Nil {
        return transaction(multi)
    }
    return reqs, err
}

multi, err := client.MultiClient()
_ = err
defer multi.Close()

watch := multi.Watch("key")
_ = watch.Err()

reqs, err := transaction(multi)
fmt.Println(err, reqs)

// Output: <nil> [SET key 1: OK]

To subscribe to the channel:

pubsub, err := client.PubSubClient()
defer pubsub.Close()

ch, err := pubsub.Subscribe("mychannel")
_ = err

subscribeMsg := <-ch
fmt.Println(subscribeMsg.Err, subscribeMsg.Name)

pub := client.Publish("mychannel", "hello")
_ = pub.Err()

msg := <-ch
fmt.Println(msg.Err, msg.Message)

// Output: <nil> subscribe
// <nil> hello

You can also write custom commands:

func Get(client *redis.Client, key string) *redis.StringReq {
    req := redis.NewStringReq("GET", key)
    client.Process(req)
    return req
}

get := Get(client, "key_does_not_exist")
fmt.Println(get.Err(), get.Val())
// Output: (nil)

Client uses connection pool to send commands. You can change maximum number of connections with:

client.ConnPool.(*redis.MultiConnPool).MaxCap = 1

Look and feel

Some corner cases:

SORT list LIMIT 0 2 ASC
client.Sort("list", redis.Sort{Offset: 0, Count: 2, Order: "ASC"})

ZRANGEBYSCORE zset -inf +inf WITHSCORES LIMIT 0 2
client.ZRangeByScoreWithScores("zset", "-inf", "+inf", 0, 2)

ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3 AGGREGATE SUM
client.ZInterStore("out", redis.ZStore{Weights: []int64{2, 3}}, "zset1", "zset2")

EVAL "return {KEYS[1],ARGV[1]}" 1 "key" "hello"
client.Eval("return {KEYS[1],ARGV[1]}", []string{"key"}, []string{"hello"})

Contributing

Configure Redis to allow maximum 10 clients:

maxclients 10

Run tests:

go test -gocheck.v

Run benchmarks:

go test -gocheck.b