Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redigo multi requests

Tags:

redis

go

redigo

I have previously been using this:

data, err := redis.Bytes(c.Do("GET", key))

to make sure that data returned is a slice of bytes.

However, I now need to add an extra command to the Redis request so I have something like this:

c.Send("MULTI")
c.Send("GET", key)
c.Send("EXPIRE", key)
r, err := c.Do("EXEC")

but now I can't seem to make the GET command return a slice of bytes. I've tried adding redis.Bytes like below but no luck.

c.Send("MULTI")
redis.Bytes(c.Send("GET", key))
c.Send("EXPIRE", key)
r, err := c.Do("EXEC")
like image 778
tommyd456 Avatar asked May 29 '15 08:05

tommyd456


1 Answers

In redis, the EXEC command returns an array containing the results of all the commands in the transaction.

redigo provides a Values function, which converts an array command reply to a []interface{}.

c.Send("MULTI")
c.Send("GET", key)
c.Send("EXPIRE", key)
r, err := redis.Values(c.Do("EXEC"))

r[0] now has the reply from your GET command as a interface{}, so you'll need to do a type assertion to get the slice of bytes you're expecting:

data := r[0].([]byte)

References

  • func Values: https://godoc.org/github.com/garyburd/redigo/redis#Values
  • Type assertions: https://golang.org/ref/spec#Type_assertions
like image 60
wyattisimo Avatar answered Oct 11 '22 06:10

wyattisimo