Type-safe Redis client for Golang
Go to file
Vladimir Mihailenco f64761880b Add NewUnixClient to connect to the unix sockets. 2013-02-02 12:50:43 +02:00
LICENSE Add license. Fixes #1. 2012-12-19 13:05:50 +02:00
README.md Typos. 2013-01-19 19:05:28 +02:00
commands.go commands: Remove Z.ScoreString method. 2012-08-25 16:34:35 +03:00
connpool.go Use net.Conn instead of io.ReadWriteCloser. 2012-08-20 14:50:25 +03:00
doc.go readme: Fix typo. 2012-08-25 21:38:36 +03:00
multi.go multi: Restrict Discard usage. 2012-08-25 22:51:42 +03:00
parser.go Ignore Read error if we read enough data. 2012-10-26 18:21:57 +03: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 Add NewUnixClient to connect to the unix sockets. 2013-02-02 12:50:43 +02:00
redis_test.go Add NewUnixClient to connect to the unix sockets. 2013-02-02 12:50:43 +02:00
req.go Add support for scripting commands. 2012-08-20 13:42:33 +03:00
req_test.go Add ParseReq method and tweak benchmarks. 2012-08-24 15:16:12 +03:00

README.md

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://go.pkgdoc.org/github.com/vmihailenco/redis

Installation

Install:

go get github.com/vmihailenco/redis

Getting started

Let's start with connecting to Redis:

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

Then we can start sending commands:

set := client.Set("foo", "bar");
if err := set.Err(); err != nil {
    panic(err)
}

get := client.Get("foo")
if err := get.Err(); err != nil {
    panic(err)
}
fmt.Println(get.Val())

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")
}
if err != nil { panic(err) }
if err := set.Err(); err != nil { panic(err) }
if err := get.Err(); err != nil { panic(err) }
fmt.Println(get.Val())
fmt.Println(reqs[0] == set)
fmt.Println(reqs[1] == get)

or:

pipeline, err := client.PipelineClient()
if err != nil {
    panic(err)
}
defer pipeline.Close()

set := pipeline.Set("key1", "hello1")
get := pipleine.Get("key2")

reqs, err := pipeline.RunQueued()
if err != nil { panic(err) }

if err := set.Err(); err != nil { panic(err) }
if err := get.Err(); err != nil { panic(err) }
fmt.Println(get.Val())
fmt.Println(reqs[0] == set)
fmt.Println(reqs[1] == get)

We can also send several commands in transaction:

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

    val, err := strconv.ParseInt(get.Val(), 10, 64)
    if err != nil { panic(err) }

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

multi, err := client.MultiClient()
if err != nil { panic(err) }
defer multi.Close()

watch := multi.Watch("key")
if err := watch.Err(); err != nil { panic(err) }

reqs, err := incrKeyInTransaction(multi)
if err != nil { panic(err) }
for _, req := range reqs {
    // ...
}

To subscribe to the channel:

pubsub, err := client.PubSubClient()
if err != nil { panic(err) }
defer pubsub.Close()

ch, err := pubsub.Subscribe("mychannel")
if err != nil { panic(err) }

go func() {
    for msg := range ch {
        if err := msg.Err; err != nil { panic(err) }
        message := msg.Message
    }
}

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(redisClient, "key")
if err := get.Err(); err != nil && err != redis.Nil { panic(err) }

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