Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query Redis db from golang using redigo library

Tags:

redis

go

redigo

I am trying to figure out what is the best way to query Redis db for multiple keys in one command. I have seen MGET which can be used for redis-cli. But how you do that using redigo library from GOlang code. Imagine I have an array of keys and I want to take from Redis db all the values for those keys in one query.

Thanks in advance!

like image 962
Daemon1313 Avatar asked Dec 11 '22 03:12

Daemon1313


2 Answers

Assuming that c is a Redigo connection and keys is a []string of your keys:

var args []interface{}
for _, k := range keys {
    args = append(args, k)
}
values, err := redis.Strings(c.Do("MGET", args...))
if err != nil {
    // handle error
}
for _, v := range values {
   fmt.Println(v)
}

The Go FAQ explains why you need to copy the keys. The spec describes how to pass a slice to a variadic param.

like image 142
Bayta Darell Avatar answered Dec 29 '22 01:12

Bayta Darell


http://play.golang.org/p/FJazj_PuCq

func main() {
    // connect to localhost, make sure to have redis-server running on the default port
    conn, err := redis.Dial("tcp", ":6379")
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close()

    // add some keys
    if _, err = conn.Do("SET", "k1", "a"); err != nil {
        log.Fatal(err)
    }
    if _, err = conn.Do("SET", "k2", "b"); err != nil {
        log.Fatal(err)
    }
    // for fun, let's leave k3 non-existing

    // get many keys in a single MGET, ask redigo for []string result
    strs, err := redis.Strings(conn.Do("MGET", "k1", "k2", "k3"))
    if err != nil {
        log.Fatal(err)
    }

    // prints [a b ]
    fmt.Println(strs)

    // now what if we want some integers instead?
    if _, err = conn.Do("SET", "k4", "1"); err != nil {
        log.Fatal(err)
    }
    if _, err = conn.Do("SET", "k5", "2"); err != nil {
        log.Fatal(err)
    }

    // get the keys, but ask redigo to give us a []interface{}
    // (it doesn't have a redis.Ints helper).
    vals, err := redis.Values(conn.Do("MGET", "k4", "k5", "k6"))
    if err != nil {
        log.Fatal(err)
    }

    // scan the []interface{} slice into a []int slice
    var ints []int
    if err = redis.ScanSlice(vals, &ints); err != nil {
        log.Fatal(err)
    }

    // prints [1 2 0]
    fmt.Println(ints)
}

UPDATE March 10th 2015: redigo now has a redis.Ints helper.

like image 40
mna Avatar answered Dec 29 '22 01:12

mna