Created
April 21, 2016 13:24
-
-
Save mdk-aza/d128d68f44244454940699213ae89ca0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/ugorji/go/codec" | |
"log" | |
"github.com/garyburd/redigo/redis" | |
"os" | |
) | |
type OtherWord struct { | |
Word []string | |
} | |
const otherWord = "*" | |
var transforms = []string{ | |
otherWord, | |
otherWord, | |
otherWord, | |
otherWord, | |
otherWord + "app", | |
otherWord + "site", | |
otherWord + "time", | |
"get" + otherWord, | |
"go" + otherWord, | |
"lets" + otherWord, | |
} | |
//msgpack専用のBundle | |
var ( | |
mh codec.MsgpackHandle | |
) | |
const message = "length of %#v as MessagePack: %d\n%v" | |
func main() { | |
otherWord := OtherWord{ | |
Word:transforms, | |
} | |
writeRedisForUseMsgPack("otherWords", messagePackEncoding(otherWord)) | |
value := readRedisForUseMsgPack("otherWords") | |
messagePackDecoding(value) | |
} | |
func messagePackEncoding(otherWord OtherWord) []byte { | |
//Encodingされる中身を受け取るbyte[]のスライス | |
buf := make([]byte, 0, 64) | |
//messagePack専用のBundleを使用してEncoderの準備をして、Encodeする。 | |
err := codec.NewEncoderBytes(&buf, &mh).Encode(otherWord) | |
if err != nil { | |
log.Printf("error encoding %v to MessagePack: %v", otherWord, err) | |
} | |
log.Printf(message, otherWord, len(buf), buf) | |
return buf; | |
} | |
func messagePackDecoding(buf []byte) OtherWord { | |
//Encodingされる中身を受け取るOtherWord構造体型の変数 | |
var otherWord OtherWord | |
//messagePack専用のBundleを使用してDecoderの準備をして、Decodeする。 | |
err := codec.NewDecoderBytes(buf, &mh).Decode(&otherWord) | |
if err != nil { | |
log.Printf("error decoding %v to MessagePack: %v", buf, err) | |
} | |
log.Printf(message, otherWord, len(buf), buf) | |
return otherWord; | |
} | |
func writeRedisForUseMsgPack(key string, buf []byte) { | |
con, err := redis.Dial("tcp", ":6379") | |
if err != nil { | |
log.Println(err) | |
os.Exit(1) | |
} | |
defer con.Close() | |
con.Do("SET", key, buf) | |
} | |
func readRedisForUseMsgPack(key string) []byte { | |
con, err := redis.Dial("tcp", ":6379") | |
if err != nil { | |
log.Println(err) | |
os.Exit(1) | |
} | |
defer con.Close() | |
buf, err := redis.Bytes(con.Do("GET", key)) | |
if err != nil { | |
log.Println(err) | |
os.Exit(1) | |
} | |
return buf; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment