Реализовано сжатие значений в redis

This commit is contained in:
ErickSkrauch 2017-06-19 02:20:38 +03:00
parent 9ffdf99b77
commit a993c1d157
2 changed files with 48 additions and 7 deletions

View File

@ -29,6 +29,7 @@ const accountIdToUsernameKey string = "hash:username-to-account-id"
func (s *SkinItem) Save() { func (s *SkinItem) Save() {
str, _ := json.Marshal(s) str, _ := json.Marshal(s)
compressedStr := tools.ZlibEncode(str)
pool, _ := services.RedisPool.Get() pool, _ := services.RedisPool.Get()
pool.Cmd("MULTI") pool.Cmd("MULTI")
@ -42,7 +43,7 @@ func (s *SkinItem) Save() {
pool.Cmd("HSET", accountIdToUsernameKey, s.UserId, s.Username) 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") pool.Cmd("EXEC")
@ -66,23 +67,33 @@ func (s *SkinItem) Delete() {
func FindSkinByUsername(username string) (SkinItem, error) { func FindSkinByUsername(username string) (SkinItem, error) {
var record SkinItem; var record SkinItem;
services.Logger.IncCounter("storage.query", 1) 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)) { if (response.IsType(redis.Nil)) {
services.Logger.IncCounter("storage.not_found", 1) services.Logger.IncCounter("storage.not_found", 1)
return record, SkinNotFound{username} return record, SkinNotFound{username}
} }
result, err := response.Str() encodedResult, err := response.Bytes()
if (err == nil) { if err == nil {
services.Logger.IncCounter("storage.found", 1) services.Logger.IncCounter("storage.found", 1)
decodeErr := json.Unmarshal([]byte(result), &record) result, err := tools.ZlibDecode(encodedResult)
if (decodeErr != nil) { if err != nil {
log.Println("Cannot decode record data") 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 record.oldUsername = record.Username
} }
finish:
return record, err return record, err
} }

30
lib/tools/zlib.go Normal file
View File

@ -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
}