From a993c1d15714e9aa8e5365830acec1242afe4bdb Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Mon, 19 Jun 2017 02:20:38 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=20=D1=81=D0=B6=D0=B0=D1=82=D0=B8=D0=B5?= =?UTF-8?q?=20=D0=B7=D0=BD=D0=B0=D1=87=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=B2=20?= =?UTF-8?q?redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/data/SkinItem.go | 25 ++++++++++++++++++------- lib/tools/zlib.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 lib/tools/zlib.go diff --git a/lib/data/SkinItem.go b/lib/data/SkinItem.go index eb88735..0e9e208 100644 --- a/lib/data/SkinItem.go +++ b/lib/data/SkinItem.go @@ -29,6 +29,7 @@ const accountIdToUsernameKey string = "hash:username-to-account-id" func (s *SkinItem) Save() { str, _ := json.Marshal(s) + compressedStr := tools.ZlibEncode(str) pool, _ := services.RedisPool.Get() pool.Cmd("MULTI") @@ -42,7 +43,7 @@ func (s *SkinItem) Save() { pool.Cmd("HSET", accountIdToUsernameKey, s.UserId, s.Username) } - pool.Cmd("SET", tools.BuildKey(s.Username), str) + pool.Cmd("SET", tools.BuildKey(s.Username), compressedStr) pool.Cmd("EXEC") @@ -66,23 +67,33 @@ func (s *SkinItem) Delete() { func FindSkinByUsername(username string) (SkinItem, error) { var record SkinItem; services.Logger.IncCounter("storage.query", 1) - response := services.RedisPool.Cmd("GET", tools.BuildKey(username)); + redisKey := tools.BuildKey(username) + response := services.RedisPool.Cmd("GET", redisKey); if (response.IsType(redis.Nil)) { services.Logger.IncCounter("storage.not_found", 1) return record, SkinNotFound{username} } - result, err := response.Str() - if (err == nil) { + encodedResult, err := response.Bytes() + if err == nil { services.Logger.IncCounter("storage.found", 1) - decodeErr := json.Unmarshal([]byte(result), &record) - if (decodeErr != nil) { - log.Println("Cannot decode record data") + result, err := tools.ZlibDecode(encodedResult) + if err != nil { + log.Println("Cannot uncompress zlib for key " + redisKey) + goto finish + } + + err = json.Unmarshal(result, &record) + if err != nil { + log.Println("Cannot decode record data for key" + redisKey) + goto finish } record.oldUsername = record.Username } + finish: + return record, err } diff --git a/lib/tools/zlib.go b/lib/tools/zlib.go new file mode 100644 index 0000000..3857052 --- /dev/null +++ b/lib/tools/zlib.go @@ -0,0 +1,30 @@ +package tools + +import ( + "io" + "bytes" + "compress/zlib" +) + +func ZlibEncode(str []byte) []byte { + var buff bytes.Buffer + writer := zlib.NewWriter(&buff) + writer.Write(str) + writer.Close() + + return buff.Bytes() +} + +func ZlibDecode(bts []byte) ([]byte, error) { + buff := bytes.NewReader(bts) + reader, readError := zlib.NewReader(buff) + if readError != nil { + return nil, readError + } + + resultBuffer := new(bytes.Buffer) + io.Copy(resultBuffer, reader) + reader.Close() + + return resultBuffer.Bytes(), nil +}