2017-06-30 21:10:25 +05:30
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"elyby/minecraft-skinsystem/model"
|
|
|
|
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/mediocregopher/radix.v2/redis"
|
|
|
|
"github.com/mediocregopher/radix.v2/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type redisDb struct {
|
|
|
|
conn util.Cmder
|
|
|
|
}
|
|
|
|
|
|
|
|
const accountIdToUsernameKey string = "hash:username-to-account-id"
|
|
|
|
|
|
|
|
func (db *redisDb) FindByUsername(username string) (model.Skin, error) {
|
|
|
|
var record model.Skin
|
2017-08-09 21:41:53 +05:30
|
|
|
if username == "" {
|
2017-08-10 05:30:02 +05:30
|
|
|
return record, &SkinNotFoundError{username}
|
2017-08-09 21:41:53 +05:30
|
|
|
}
|
|
|
|
|
2017-06-30 21:10:25 +05:30
|
|
|
redisKey := buildKey(username)
|
|
|
|
response := db.conn.Cmd("GET", redisKey)
|
|
|
|
if response.IsType(redis.Nil) {
|
2017-08-10 05:30:02 +05:30
|
|
|
return record, &SkinNotFoundError{username}
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
encodedResult, err := response.Bytes()
|
|
|
|
if err == nil {
|
|
|
|
result, err := zlibDecode(encodedResult)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Cannot uncompress zlib for key " + redisKey)
|
2017-08-10 05:30:02 +05:30
|
|
|
return record, err
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(result, &record)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Cannot decode record data for key" + redisKey)
|
2017-08-10 05:30:02 +05:30
|
|
|
return record, nil
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
record.OldUsername = record.Username
|
|
|
|
}
|
|
|
|
|
2017-08-10 05:30:02 +05:30
|
|
|
return record, nil
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (db *redisDb) FindByUserId(id int) (model.Skin, error) {
|
|
|
|
response := db.conn.Cmd("HGET", accountIdToUsernameKey, id)
|
|
|
|
if response.IsType(redis.Nil) {
|
2017-07-02 06:05:38 +05:30
|
|
|
return model.Skin{}, SkinNotFoundError{"unknown"}
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
username, _ := response.Str()
|
|
|
|
|
|
|
|
return db.FindByUsername(username)
|
|
|
|
}
|