简体   繁体   English

使用 redigo 读取 Redis 键值,即 JSON 字符串

[英]Reading Redis key-value which is JSON string using redigo

I am trying to read Redis Key-val in Go.我正在尝试在 Go 中读取 Redis Key-val。 Key is string and value is JSON string.键是字符串,值是 JSON 字符串。 Eg- Key=例如-键=

discov_32161296

and Value as Json string=和值作为 Json 字符串=

"{\"10283\":true,\"11064\":true,\"15123\":true,\"15447\":true,\"15926\":true,\"16530\":true,\"16537\":true,\"16799\":true,\"17088\":true,\"17249\":true,\"18501\":true,\"18529\":true,\"18601\":true,\"3044\":true,\"3687\":true,\"4926\":true,\"5483\":true,\"6\":true,\"6675\":true,\"8332\":true,\"8336\":true,\"8674\":true}"

Getting below error while reading in Go在 Go 中阅读时出现以下错误

redis.Values err redigo: unexpected type for Values, got type []uint8

Here's my code :这是我的代码:

uIDDiscoveryOffer := fmt.Sprintf("%s_%d", "discov", uid)
opDataStr, err := redis.String(redis.Values(con.Do("GET", uIDDiscoveryOffer)))
    if err != nil || err != redis.ErrNil {
        utils.Log1("readCacheTxnByUID-Disc-redis.Values-err", fmt.Sprint("redis.Values err : ", uidDiscoveryOffer, " error: ", err.Error()))
    } else {
         //Some Logic
    }

The Redis GET returns the value of a key. Redis GET返回键的值。 redis.Values() may be used to convert the result of a command that returns multiple items. redis.Values()可用于转换返回多个项目的命令的结果。

Since GET returns a single item, only useredis.String() , you don't need redis.Values() here:由于GET返回单个项目,因此只使用redis.String() ,这里不需要redis.Values()

opDataStr, err := redis.String(con.Do("GET", uIDDiscoveryOffer))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM