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 ADS生成的微带线为什么是蓝色空心的
  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭