1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| func (c RedisCache)Set(item *RankItem) error{ if err := c.client.Ping().Err(); err != nil { return err } err := c.client.ZAdd(pveStarRankRedisKey, redis.Z{Score: item.Value(), Member: item.PlayerID}).Err() if err != nil { } return err }
func (c RedisCache)Get() ([]RankItem, error) { if err := c.client.Ping().Err(); err != nil { return nil, err }
results, err := c.client.ZRevRangeWithScores(pveStarRankRedisKey, 0, 99).Result() if err != nil { return nil, err } items := make([]model.RankItem, 0, len(results)) for _, v := range results { playerID := v.Member.(string) value := int(v.Score) items = append(items, model.RankItem{ PlayerID: playerID, Score: value, }) } return items, nil }
func (c RedisCache)Del(maxRanksNum int64) (int64, error){ if err := c.client.Ping().Err(); err != nil { return 0, err }
num, err := c.client.ZCard(dbKey).Result() if err != nil { return 0, err } availDeleteNum := num - maxRanksNum if availDeleteNum <= 0 { return 0, nil } return c.client.ZRemRangeByRank(dbKey, 0, availDeleteNum-1).Result() }
|