0

I'm new to go programming language and just wanted to write a small web app project with a good architecture.

I get some specific recangle objects through a post request.

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

I decided to choose redis for storing because it has high performance and I wanted to get my hands on that.

I have a bit confusion here :

  1. There is no specific key in the object's nature for storing, so I came up with the idea of auto incremention of keys but still stuck how to that because as I studied the pattern for insertion, it's something like:

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

As you can see, I'm stuck what to write in the key field. I saw the Redis commands "INCR" but it seems none make sense for this pattern.

  1. If I want to get all rectangles regardless of their keys, will connection.Do("HGETALL", "*", rectangles[]) command help me get an array of all rectangles in the databse?
Dave C
  • 7,729
  • 4
  • 49
  • 65
  • I think you're looking for the `INCR` command? Possible dupe of: https://stackoverflow.com/q/24028171/660921 – You might be better off using GUIDs if you want a globally unique identifier btw, since you won't have to worry about race conditions where two objects will have the same ID. – Martin Tournoij Aug 03 '19 at 12:52
  • Yes, I'm looking for the same thing but wondering how to implement it using redigo.(I prefer to bulk insert my array of struct). Plus I'm wondering how to retrieve all values as well. – chameleon123 Aug 03 '19 at 13:29
  • 1
    Redigo is a very thin wrapper around Redis and little more than a fancy API to send text commands to Redis. What specifically are you having problems with? It would be helpful if you could share some of the code you have ([mcve]). – Martin Tournoij Aug 03 '19 at 13:43
  • I added the code snippet above. My problem is what to place in the key field? "INCR"? The object is a rectangle with x,y start coordination and its width and height. – chameleon123 Aug 03 '19 at 14:02
  • And if I pass my array as bulk, will it understand to divide it in objects and assign the appropriate keys? – chameleon123 Aug 03 '19 at 14:21
  • I tried my best to clarify the problem :) – chameleon123 Aug 03 '19 at 15:04
  • A [list](https://redis.io/topics/data-types#lists) meets the requirements set forth in the question. Add rectangle: `_, err := c.Do("RPUSH", "rectangles", rectJSON)`. Get all rectangles: `rectJSONs, err := redis.ByteSlices(c.Do("LRANGE" "rectangles", 0, -1))` – Charlie Tumahai Aug 03 '19 at 15:12
  • Thank you, it worked. Just needed another conversion after getting byte slices. – chameleon123 Aug 03 '19 at 18:35

1 Answers1

0

A list meets the requirements set forth in the question.

Add rectangle:

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

Get all rectangles:

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)
}
Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242