Last active
March 16, 2021 20:15
-
-
Save nassor/8c7cc8007f5f471671041927f789a8c2 to your computer and use it in GitHub Desktop.
Zlib Compress in Go -> Redis List -> Zlib Uncompress in Ruby
This file contains hidden or 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 ( | |
| "bytes" | |
| "compress/zlib" | |
| "fmt" | |
| "time" | |
| "github.com/vmihailenco/msgpack" | |
| redis "gopkg.in/redis.v5" | |
| ) | |
| type Payload struct { | |
| Data []string | |
| } | |
| func main() { | |
| rc := redis.NewClient(&redis.Options{ | |
| Addr: "127.0.0.1:6379", | |
| Password: "", | |
| DB: 0, | |
| }) | |
| rc.FlushAll() | |
| for i := 0; i < 10000000; i++ { | |
| p := &Payload{Data: []string{fmt.Sprint(10 + i), fmt.Sprint(100 + i), fmt.Sprint(1000 + i)}} | |
| data := compress(pack(p)) | |
| redisPush(rc, "compressed-data-pipeline", data) | |
| time.Sleep(200 * time.Millisecond) | |
| } | |
| } | |
| func compress(b []byte) []byte { | |
| var buffer bytes.Buffer | |
| w := zlib.NewWriter(&buffer) | |
| w.Write(b) | |
| w.Close() | |
| return buffer.Bytes() | |
| } | |
| func pack(p *Payload) []byte { | |
| b, err := msgpack.Marshal(p) | |
| if err != nil { | |
| panic(err) | |
| } | |
| return b | |
| } | |
| func redisPush(rc *redis.Client, key string, data []byte) { | |
| rc.RPush(key, data) | |
| } |
This file contains hidden or 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
| require "zlib" | |
| require "redis" | |
| require 'msgpack' | |
| redis = Redis.new | |
| def decompress(data) | |
| uncompressed_data = Zlib::Inflate.inflate(data) | |
| return uncompressed_data | |
| end | |
| def unpack(data) | |
| return MessagePack.unpack(data) | |
| end | |
| while true do | |
| list, data = redis.blpop("compressed-data-pipeline", :timeout => 5) | |
| if list == nil then | |
| break | |
| end | |
| d = unpack(decompress(data)) | |
| print d['Data'][0] + '|' + d['Data'][1] + '|' + d['Data'][2] | |
| print "\n" | |
| end |
This file contains hidden or 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
| version: '3' | |
| services: | |
| redis: | |
| image: redis | |
| ports: | |
| - "127.0.0.1:6379:6379" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment