dtsi9484 2016-08-20 08:39
浏览 65
已采纳

Redigo和Gob如何获取Gob数据切片

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)

  • 写回答

1条回答 默认 最新

  • douyao1994 2016-08-20 13:21
    关注

    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())
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下