MCPcopy
hub / github.com/redis/go-redis / topK

Function topK

example/redis-bloom/main.go:121–155  ·  view source on GitHub ↗
(ctx context.Context, rdb *redis.Client)

Source from the content-addressed store, hash-verified

119}
120
121func 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}

Callers 1

mainFunction · 0.85

Calls 5

StringSliceMethod · 0.80
Int64SliceMethod · 0.80
ErrMethod · 0.65
DoMethod · 0.65
PrintfMethod · 0.65

Tested by

no test coverage detected