Redigo多请求

huangapple go评论100阅读模式
英文:

Redigo multi requests

问题

我之前一直使用以下代码来确保返回的数据是一个字节切片:

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

然而,现在我需要在Redis请求中添加一个额外的命令,所以代码变成了这样:

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

但是现在我似乎无法让GET命令返回一个字节切片。我尝试了在下面的代码中添加redis.Bytes,但没有成功:

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

请帮我看看如何解决这个问题。谢谢!

英文:

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")

答案1

得分: 6

在Redis中,EXEC命令返回一个包含事务中所有命令结果的数组。

redigo提供了一个Values函数,用于将数组命令的回复转换为[]interface{}

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

现在,r[0]中包含了你的GET命令的回复作为一个interface{},所以你需要进行类型断言来获取你期望的字节切片:

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

参考资料

  • Values函数:https://godoc.org/github.com/garyburd/redigo/redis#Values
  • 类型断言:https://golang.org/ref/spec#Type_assertions
英文:

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

答案2

得分: 4

MULTI用于以原子方式向Redis发送多个命令,创建一个事务。这与管道完全不同。

在调用EXEC之前,所有的命令都不会被实际执行,因此在事务内部调用GET时无法获取值。

根据文档:

> 当Redis连接处于MULTI请求的上下文中时,所有命令都将回复字符串"QUEUED"(从Redis协议的角度来看,作为状态回复发送)。排队的命令只是在调用EXEC时被安排执行。

在redigo中,管道传输是以不同的方式进行的:

http://godoc.org/github.com/garyburd/redigo/redis#hdr-Pipelining

你想要做的是像这样(未经测试):

c.Send("GET", key)
c.Send("EXPIRE", key)
c.Flush()
v := redis.Bytes(c.Receive()) // GET的回复
_, err = c.Receive() // EXPIRE的回复
英文:

MULTI is used to send several commands in an atomic way to Redis, by creating a transaction. This is not a pipeline at all.

None of the commands will be actually executed before the EXEC call so it is impossible to obtain the value when you call GET from within a transaction.

From the docs:

> When a Redis connection is in the context of a MULTI request, all commands will reply with the string QUEUED (sent as a Status Reply from the point of view of the Redis protocol). A queued command is simply scheduled for execution when EXEC is called.

In redigo pipelining is done in a different way:

http://godoc.org/github.com/garyburd/redigo/redis#hdr-Pipelining

What you want to do is something like this (untested):

c.Send("GET", key)
c.Send("EXPIRE", key)
c.Flush()
v := redis.Bytes(c.Receive()) // reply from GET
_, err = c.Receive() // reply from EXPIRE

huangapple
  • 本文由 发表于 2015年5月29日 16:39:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/30525005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定