Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

存redis数据库问题!

//用户的消息存储
func (this *UserDao) SaveContent(id int, content string) (err error) {
    // 先从UserDao 的连接池中取出一根连接
    conn := this.Pool.Get()
    defer conn.Close()
    nowTime := time.Now()
    noTime := nowTime.Format("2006-01-02 15:04:05")

    //定义map存储内容和时间
    saveContent := make(map[string]string)
    saveContent[noTime] = content

    data, err := json.Marshal(saveContent)
    if err != nil {
        fmt.Println("json.Marshal(saveContent) err=", err)
    }
    //入库
    _, err = conn.Do("HSet", "usersContent", id, string(data))
    if err != nil {
        fmt.Println("HSet err =", err)
    }
    return
}

* * *

本段代码,我定义了一个map,想把当前时间作为键,把用户的消息内容为值存入redis,结果如下:
image.png
,但是用户下一次发消息的时候,会覆盖上一条数据,每个用户就只有一条数据。请问该咋改勒!谢谢啦### 问题描述


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

可以使用list结构, sortedset也可以(具体看需求)

// _, err = conn.Do("LPUSH", "usersContent:{id}", string(data))
_, err = conn.Do("RPUSH", "usersContent:{id}", string(data))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...