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
|
/**
* @description: 有序集合添加
* @user: Shershon
* @receiver r RedisController
*/
func (r RedisController) SortSet() error {
opType := r.GetParam("opType")
key := r.GetParam("key")
if opType == "1" {
// 有序集合添加
zs := []*redis.Z{
{Member: "小张",Score: 88},
{Member: "小李",Score: 90},
{Member: "小明",Score: 80},
{Member: "小英",Score: 70},
{Member: "小赵",Score: 95},
{Member: "小王",Score: 75},
{Member: "笨蛋",Score: 40},
}
result, err := RedisClient.ZAdd(ctx, key, zs...).Result()
if err != nil {
return r.Error(err.Error())
}
return r.Success(result)
} else {
resultMap := make(map[string]interface{})
// 获取成员数
val := RedisClient.ZCard(ctx, key).Val()
resultMap["获取成员数"] = val
// 获取指定分数区间的成员数
resultMap["70分-90分成员数"] = RedisClient.ZCount(ctx, key, "70", "90").Val()
// 返回有序集中指定分数区间内的成员,分数从高到低排序
result, _ := RedisClient.ZRevRangeByScoreWithScores(ctx, key, &redis.ZRangeBy{
Min: "0", Max: "100", Offset: 0, Count: 3,
}).Result()
resultMap["分数前三排名"] = result
// 返回分数值
f, _ := RedisClient.ZScore(ctx, key, "小张").Result()
resultMap["小张的分数"] = f
// 给笨蛋加60分
f2, err := RedisClient.ZIncrBy(ctx, key, 60.0, "笨蛋").Result()
fmt.Println(err)
fmt.Printf("新分数:%v \n",f2)
result2, _ := RedisClient.ZRevRangeByScoreWithScores(ctx, key, &redis.ZRangeBy{
Min: "0", Max: "100", Offset: 0, Count: 3,
}).Result()
resultMap["调整分后排名"] = result2
return r.Success(resultMap)
}
|