mirror of
https://github.com/elyby/chrly.git
synced 2024-12-23 21:50:03 +05:30
Реализовано сжатие значений в redis
This commit is contained in:
parent
9ffdf99b77
commit
a993c1d157
@ -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
|
||||
}
|
||||
|
||||
|
30
lib/tools/zlib.go
Normal file
30
lib/tools/zlib.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user