Home > Software engineering >  go redis HMSet fail
go redis HMSet fail

Time:11-03

When I use redis hmset in Go I get the following problem, why is it? ERR wrong number of arguments for 'hset' command Resulting in values not being stored in redis ? I'm referring to the redis book, why is this a problem ?

func (r *ArticleRepo) PostArticle(user, title, link string) string {
    articleId := strconv.Itoa(int(r.Conn.Incr("article:").Val()))

    voted := "voted:"   articleId
    r.Conn.SAdd(voted, user)
    r.Conn.Expire(voted, common.OneWeekInSeconds*time.Second)

    now := time.Now().Unix()
    article := "article:"   articleId
    _, err := r.Conn.HMSet(article, map[string]interface{}{
        "title":  title,
        "link":   link,
        "poster": user,
        "time":   now,
        "votes":  1,
    }).Result()
    if err != nil {
        fmt.Println(err)
    }

    r.Conn.ZAdd("score:", &redis.Z{Score: float64(now   common.VoteScore), Member: article})
    r.Conn.ZAdd("time:", &redis.Z{Score: float64(now), Member: article})
    return articleId
}

CodePudding user response:

You can use hset instead of hmset with something like this in Go:

 _, err := r.Conn.Do("hset", article, map[string]interface{}{
    "title":  title,
    "link":   link,
    "poster": user,
    "time":   now,
    "votes":  1,
}).Result()
  • Related