1

I'm pushing in my redis base my objects with the "RPUSH" command.

// object is of type interface

var network bytes.Buffer
gob.NewEncoder(&network)
enc.Encode(object /* interface{} */)

redis.String(d.Conn.Do("RPUSH", "objects", network.String()))

Redigo do what i'm expecting, it is pushing all the data structure gob-encoded.

Now I'm trying to retreive them:

sall, _ := redis.Strings(d.Conn.Do("LRANGE", "todos", "0", "-1"))
fmt.Printf("%T", sall) // type []string as expected

// At this point, I have no idea if I should store the data in a buffer, or convert it directly as bytes. actually, here I'm lost
var network bytes.Buffer
var object []interface{}


dec := gob.NewDecoder(network)
err := dec.Decode(inout)
fmt.Printf("%v", err) // decode error:EOF

What is the best way to gob-decod them? I'd want to get them back as a slice of interface{}. But even if my object are encoded as gob data. They are pushed in the redis way, so can it be considered as a slice from the point view of gob?

I could iterate other the list and decode them one by one. But I'm not confident about the efficiency. I'm assuming gob want a slice structure encoded in its way. So my question is: Is there a trick in order to decode my slice of gob data efficiently as a collection of data structure? Or should I store my data structure in another way (I'm assuming storing my data with RPUSH can prevent non-attomic manipulation)

Mr Bonjour
  • 3,330
  • 2
  • 23
  • 46

1 Answers1

3

The LRANGE command returns a list. Use redis.ByteSlices to get that list as a [][]byte. Decode each gob in the list:

items, err := redis.ByteSlices(d.Conn.Do("LRANGE", "objects", "0", "-1"))
if err != nil {
   // handle error
}
var values []*Object
for _, item := range items {
    var v Object
    if err := gob.NewDecoder(bytes.NewReader(item)).Decode(&v); err != nil {
        // handle error
    }
    values = append(values, &v)
}

This assumes that a new gob.Encoder was created for each value pushed into the list.

If the application does not access the list items independently in Redis, then gob encode the entire list and store it as a bulk string:

 var values []*Object
 var buf bytes.Buffer
 if err := gob.NewEncoder(&buf).Encode(values); err != nil {
     // handle error
 }
 if _, err := d.Conn.Do("SET", "objects", buf.Bytes()); err != nil {
     // handler error
 }

Here's how to decode it:

items, err := redis.Bytes(d.Conn.Do("GET", "objects"))
if err != nil {
    // handle error
}
var values []*Objects
if err := gob.NewDecoder(items).Decode(&values); err != nil {
    // handle error
}

Here's an aside on this line of code from the question:

 redis.String(d.Conn.Do("RPUSH", "objects", network.String()))

Use network.Bytes() to avoid the string allocation. Use redis.Int to decode the integer return value from RPUSH. Write the code as:

 n, err := redis.Int(d.Conn.Do("RPUSH", "objects", network.Bytes()))

or if you don't care about the count of elements returned from the list, write it as:

 _, err := d.Conn.Do("RPUSH", "objects", network.Bytes())
Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242