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
| rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", DB: 0, PoolSize: 20, }) ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel()
zsetKey := "scores_rank"
rank := []*redis.Z{ {Score: 90.0, Member: "Alice"}, {Score: 90.1, Member: "Bob"}, {Score: 80.2, Member: "Cick"}, {Score: 89.2, Member: "David"}, {Score: 99.1, Member: "Eric"}, {Score: 78.1, Member: "Frank"}, } if err := rdb.ZAdd(ctx, zsetKey, rank...).Err(); err != nil { fmt.Println("zadd failed", err) return } fmt.Println("zadd success")
newscore, err := rdb.ZIncrBy(ctx, zsetKey, -10, "Frank").Result() if err != nil { fmt.Println("zincrby failed", err) return } fmt.Printf("Frank's score is %f\n", newscore)
res := rdb.ZRevRangeWithScores(ctx, zsetKey, 0, 2).Val() fmt.Println("ZRevRangeWithScores's res :", res)
for _, z := range res { fmt.Println(z) }
|