|
| 1 | +// EXAMPLE: cms_tutorial |
| 2 | +// HIDE_START |
| 3 | +package example_commands_test |
| 4 | + |
| 5 | +import ( |
| 6 | +"context" |
| 7 | +"fmt" |
| 8 | + |
| 9 | +"github.com/redis/go-redis/v9" |
| 10 | +) |
| 11 | + |
| 12 | +// HIDE_END |
| 13 | + |
| 14 | +func ExampleClient_cms() { |
| 15 | +ctx := context.Background() |
| 16 | + |
| 17 | +rdb := redis.NewClient(&redis.Options{ |
| 18 | +Addr: "localhost:6379", |
| 19 | +Password: "", // no password docs |
| 20 | +DB: 0, // use default DB |
| 21 | +}) |
| 22 | + |
| 23 | +// REMOVE_START |
| 24 | +rdb.Del(ctx, "bikes:profit") |
| 25 | +// REMOVE_END |
| 26 | + |
| 27 | +// STEP_START cms |
| 28 | +res1, err := rdb.CMSInitByProb(ctx, "bikes:profit", 0.001, 0.002).Result() |
| 29 | + |
| 30 | +if err != nil { |
| 31 | +panic(err) |
| 32 | +} |
| 33 | + |
| 34 | +fmt.Println(res1) // >>> OK |
| 35 | + |
| 36 | +res2, err := rdb.CMSIncrBy(ctx, "bikes:profit", |
| 37 | +"Smoky Mountain Striker", 100, |
| 38 | +).Result() |
| 39 | + |
| 40 | +if err != nil { |
| 41 | +panic(err) |
| 42 | +} |
| 43 | + |
| 44 | +fmt.Println(res2) // >>> [100] |
| 45 | + |
| 46 | +res3, err := rdb.CMSIncrBy(ctx, "bikes:profit", |
| 47 | +"Rocky Mountain Racer", 200, |
| 48 | +"Cloudy City Cruiser", 150, |
| 49 | +).Result() |
| 50 | + |
| 51 | +if err != nil { |
| 52 | +panic(err) |
| 53 | +} |
| 54 | + |
| 55 | +fmt.Println(res3) // >>> [200 150] |
| 56 | + |
| 57 | +res4, err := rdb.CMSQuery(ctx, "bikes:profit", |
| 58 | +"Smoky Mountain Striker", |
| 59 | +).Result() |
| 60 | + |
| 61 | +if err != nil { |
| 62 | +panic(err) |
| 63 | +} |
| 64 | + |
| 65 | +fmt.Println(res4) // >>> [100] |
| 66 | + |
| 67 | +res5, err := rdb.CMSInfo(ctx, "bikes:profit").Result() |
| 68 | + |
| 69 | +if err != nil { |
| 70 | +panic(err) |
| 71 | +} |
| 72 | + |
| 73 | +fmt.Printf("Width: %v, Depth: %v, Count: %v", |
| 74 | +res5.Width, res5.Depth, res5.Count) |
| 75 | +// >>> Width: 2000, Depth: 9, Count: 450 |
| 76 | +// STEP_END |
| 77 | + |
| 78 | +// Output: |
| 79 | +// OK |
| 80 | +// [100] |
| 81 | +// [200 150] |
| 82 | +// [100] |
| 83 | +// Width: 2000, Depth: 9, Count: 450 |
| 84 | +} |
0 commit comments