func (this *PlayerInfo) Save(redis *redis.Client) error { buf := bytes.NewBuffer(nil) enc := gob.NewEncoder(buf) err := enc.Encode(this) if err == nil { err = redis.Set(this.ID, buf.Bytes()) } return err }
//序列化测试 func (this *PlayerInfo) ZAdd(redis *redis.Client, key string, i float64) error { buf := bytes.NewBuffer(nil) enc := gob.NewEncoder(buf) err := enc.Encode(this) if err == nil { _, err := redis.Zadd(key, buf.Bytes(), i) return err } return nil }
func LoadPlayer(redis *redis.Client, id string) *PlayerInfo { data, err := redis.Get(id) if err == nil { playerInfo := new(PlayerInfo) buf := bytes.NewBuffer(data) dec := gob.NewDecoder(buf) dec.Decode(playerInfo) return playerInfo } return nil }
//排序 func ZRange(redis *redis.Client, key string) { //items, _ := redis.Zrangebyscore(key, 0, 100) //取出所有拍卖的物品 //items, _ := redis.Zrevrange(key, 0, -1) items, _ := redis.ZMyGet(key, "0", "100", "2") fmt.Println("items.len = ", len(items), items) player_info := make([]PlayerInfo, len(items)) for k, data := range items { DecodeItem(data, &player_info[k]) fmt.Println("player_info[k].Gold = ", player_info[k].Gold, "player_info[k].ID = ", player_info[k].ID) } }
func DeletePlayerInfo(redis *redis.Client, id string) (bool, error) { return redis.Del(id) }