是否有任何自动增量机制用于使用 redigo 包装器将键值存储在 redis 中?

Is there any auto incremention machanim for storing keys' values in redis with redigo wrapper?

我是编程语言的新手,只想编写一个具有良好架构的小型 Web 应用程序项目。

我通过 post 请求获得了一些特定的 recangle 对象。

type Rectangle struct {
X         int //starting x coordinate
Y         int //starting y coordinate
Width     int
Height    int
CreatedAt time.Time
}

我决定选择redis来存储,因为它性能高,我想动手。

我有点困惑:

  1. 对象的存储性质没有特定的键,所以我想出了键自动递增的想法,但仍然坚持如何去做,因为在我研究插入模式时,它是类似于:

    json, err := json.Marshal(rectangle)
    if err != nil {
        return err
    }
    
    _, err = connection.Do("SET", key, json)
    if err != nil {
        return err
    }
    

如您所见,我不知道要在关键字段中写什么。我看到了 Redis 命令 "INCR" 但似乎 none 对这种模式有意义。

  1. 如果我想获取所有矩形而不考虑它们的键,connection.Do("HGETALL", "*", rectangles[]) 命令会帮助我获取数据库中所有矩形的数组吗?

A​​ list符合问题中提出的要求。

添加矩形:

 _, err := c.Do("RPUSH", "rectangles", rectJSON). 

获取所有矩形:

rectJSONs, err := redis.ByteSlices(c.Do("LRANGE" "rectangles", 0, -1))
if err != nil {
   // handle error
}
var rectangles []*Rectangle
for _, rj := range rectJSONS {
   var r Rectangle
   if err := json.Unmarshal(rj, &r); err != nil {
       // handle error
   }
   rectangles = append(rectangles, &r)
}