Skip to content

Instantly share code, notes, and snippets.

@edvakf edvakf/main.go
Created Sep 26, 2014

Embed
What would you like to do?
gob serialize
package main
import (
"bytes"
"database/sql"
"encoding/gob"
"log"
Radix "github.com/fzzy/radix/extra/pool"
)
var radix *Radix.Pool
type Stock struct {
ID uint
VariationID uint `db:"variation_id"`
SeatID string `db:"seat_id"`
OrderID sql.NullInt64 `db:"order_id"`
UpdatedAt string `db:"updated_at"`
}
func main() {
var err error
radix, err = Radix.NewPool("tcp", ":6379", 10)
if err != nil {
log.Panicf("error initializing redis: %s", err.Error())
}
redisSet()
redisGet()
}
func redisSet() {
redis, err := radix.Get()
if err != nil {
panic(err.Error())
}
defer radix.Put(redis)
s := Stock{
ID: 10,
VariationID: 20,
SeatID: "aaabbb",
OrderID: sql.NullInt64{10, true},
UpdatedAt: "2014-09-25 01:23:45",
}
log.Printf("%+v", s)
b := new(bytes.Buffer)
enc := gob.NewEncoder(b)
err = enc.Encode(s)
if err != nil {
panic(err.Error())
}
out := b.Bytes()
_, err = redis.Cmd("LPUSH", "mykey", out).Int()
if err != nil {
panic(err.Error())
}
}
func redisGet() {
redis, err := radix.Get()
if err != nil {
panic(err.Error())
}
defer radix.Put(redis)
var s Stock
res, err := redis.Cmd("RPOP", "mykey").Bytes()
if err != nil {
panic(err.Error())
}
in := bytes.NewBuffer(res)
dec := gob.NewDecoder(in)
err = dec.Decode(&s)
if err != nil {
panic(err.Error())
}
log.Printf("%+v", s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.