(ctx context.Context, rdb *redis.Client)
| 119 | } |
| 120 | |
| 121 | func topK(ctx context.Context, rdb *redis.Client) { |
| 122 | if err := rdb.Do(ctx, "TOPK.RESERVE", "top_items", 3).Err(); err != nil { |
| 123 | panic(err) |
| 124 | } |
| 125 | |
| 126 | counts := map[string]int{ |
| 127 | "item1": 1000, |
| 128 | "item2": 2000, |
| 129 | "item3": 3000, |
| 130 | "item4": 4000, |
| 131 | "item5": 5000, |
| 132 | "item6": 6000, |
| 133 | } |
| 134 | |
| 135 | for item, count := range counts { |
| 136 | for i := 0; i < count; i++ { |
| 137 | if err := rdb.Do(ctx, "TOPK.INCRBY", "top_items", item, 1).Err(); err != nil { |
| 138 | panic(err) |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | items, err := rdb.Do(ctx, "TOPK.LIST", "top_items").StringSlice() |
| 144 | if err != nil { |
| 145 | panic(err) |
| 146 | } |
| 147 | |
| 148 | for _, item := range items { |
| 149 | ns, err := rdb.Do(ctx, "TOPK.COUNT", "top_items", item).Int64Slice() |
| 150 | if err != nil { |
| 151 | panic(err) |
| 152 | } |
| 153 | fmt.Printf("%s: top-k=%d actual=%d\n", item, ns[0], counts[item]) |
| 154 | } |
| 155 | } |
no test coverage detected