Go-Redis的HMSet在使用字符串字段时出现WRONGTYPE操作错误。

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

Go-Redis HMSet with string fields gives WRONGTYPE Operation

问题

我正在尝试使用HMSET来设置一个包含两个字符串字段(idcontent)的新hash

我可以很容易地通过redis-cli来实现这一点,使用SET i 0来初始化一个计数器用于ids,然后使用HMSET test id hey content herro创建一个新的hash,并使用HMGET test id content获取这两个字段,结果是1) hey 2) herro

不幸的是,我无法通过Go-Redis以及特别是HMSet来实现这样的结果。

到目前为止,我尝试过以下代码:

var uid = "0"
err = c.Get("i").Err()
if(err != nil) {
    //如果计数器未设置,则将其设置为0
	err := c.Set("i", "0", 0).Err()
    if(err != nil){
	    panic(err)
	}
} else {
    //否则,递增计数器
	counter, err := c.Incr("i").Result()
    //将其转换为字符串
	uid = strconv.FormatInt(index, 10)
    if(err != nil){
	    panic(err)
	}
    //设置新的计数器
	err = c.Set("i", counter, 0).Err()
	if( err!= nil ){
	    panic(err)
	}
}

//初始化一个map[string]interface{}
var m = make(map[string]interface{})
m["id"] = uid
m["content"] = "herro"

hash, err := c.HMSet("i", m).Result()
if(err != nil){
	panic(err)
}

fmt.Println(hash)

除了c.HMSet("i", m).Result()之外,其他都正常工作。我得到了以下错误信息:

> WRONGTYPE Operation against a key holding the wrong kind of value

我真的不明白为什么会出现这个错误,因为我在redis-cli中以完全相同的方式使其工作。

HMSet的定义为func (c *Client) HMSet(key string, fields map[string]interface{}) *StatusCmd

我无法找到任何使用Go-Redis的在线示例来说明这种用法。

我做错了什么?

英文:

I'm trying to use HMSET to set a new hash containing two string fields, id and content.

I'm able to make this via redis-cli pretty easily using SET i 0 to initialize a counter for ids, then creating a new hash using HMSET test id hey content herro and getting both of these fields with HMGET test id content resulting in 1) hey 2) herro.

Unfortunately I'm not able to achieve such result with Go-Redis and in particular with HMSet.

So far I tried

var uid = "0"
err = c.Get("i").Err()
if(err != nil) {
    //If the counter is not set, set it to 0
	err := c.Set("i", "0", 0).Err()
    if(err != nil){
	    panic(err)
	}
} else {
    //Else, increment it
	counter, err := c.Incr("i").Result()
    //Cast it to string
	uid = strconv.FormatInt(index, 10)
    if(err != nil){
	    panic(err)
	}
    //Set new counter
	err = c.Set("i", counter, 0).Err()
	if( err!= nil ){
	    panic(err)
	}
}

//Init a map[string]interface{}
var m = make(map[string]interface{})
m["id"] = uid
m["content"] = "herro"

hash, err := c.HMSet("i", m).Result()
if(err != nil){
	panic(err)
}

fmt.Println(hash)

Everything works fine but c.HMSet("i", m).Result(). I get:

> WRONGTYPE Operation against a key holding the wrong kind of value

And I cannot really get why since I managed to make it work on the very same way in redis-cli.

HMSet is defined as func (c *Client) HMSet(key string, fields map[string]interface{}) *StatusCmd.

I wasn't able to find any example online using Go-Redis illustrating this use case.

What Am I doing Wrong?

答案1

得分: 3

你正在两次访问相同的键"i" - 一次是在调用SET时作为字符串,然后在调用HMSET时作为哈希。你得到的错误只是Redis拒绝在字符串上执行HMSET操作,这是无效的操作。

顺便说一句,反过来是可以的 - 在Redis中对任何类型调用SET只会写入一个字符串,而不是该值,所以要小心。

英文:

You are accessing the same key "i" twice - once as a string when calling SET, and then as a hash when calling HMSET.

The error you are getting is just redis denying HMSET on a string, which is an invalid operation.

BTW the other way around will work - calling SET on any type in redis will just write a string instead of that value, so be careful.

huangapple
  • 本文由 发表于 2017年5月3日 04:40:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/43746853.html
匿名

发表评论

匿名网友

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

确定