chrly/db/skins/redis/commands.go

61 lines
1.3 KiB
Go

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
if username == "" {
return record, &SkinNotFoundError{username}
}
redisKey := buildKey(username)
response := db.conn.Cmd("GET", redisKey)
if response.IsType(redis.Nil) {
return record, &SkinNotFoundError{username}
}
encodedResult, err := response.Bytes()
if err == nil {
result, err := zlibDecode(encodedResult)
if err != nil {
log.Println("Cannot uncompress zlib for key " + redisKey)
return record, err
}
err = json.Unmarshal(result, &record)
if err != nil {
log.Println("Cannot decode record data for key" + redisKey)
return record, nil
}
record.OldUsername = record.Username
}
return record, nil
}
func (db *redisDb) FindByUserId(id int) (model.Skin, error) {
response := db.conn.Cmd("HGET", accountIdToUsernameKey, id)
if response.IsType(redis.Nil) {
return model.Skin{}, SkinNotFoundError{"unknown"}
}
username, _ := response.Str()
return db.FindByUsername(username)
}