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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?