...

Source file src/github.com/redis/go-redis/v9/doctests/cms_tutorial_test.go

Documentation: github.com/redis/go-redis/v9/doctests

     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  	// make sure we are working with fresh database
    25  	rdb.FlushDB(ctx)
    26  	rdb.Del(ctx, "bikes:profit")
    27  	// REMOVE_END
    28  
    29  	// STEP_START cms
    30  	res1, err := rdb.CMSInitByProb(ctx, "bikes:profit", 0.001, 0.002).Result()
    31  
    32  	if err != nil {
    33  		panic(err)
    34  	}
    35  
    36  	fmt.Println(res1) // >>> OK
    37  
    38  	res2, err := rdb.CMSIncrBy(ctx, "bikes:profit",
    39  		"Smoky Mountain Striker", 100,
    40  	).Result()
    41  
    42  	if err != nil {
    43  		panic(err)
    44  	}
    45  
    46  	fmt.Println(res2) // >>> [100]
    47  
    48  	res3, err := rdb.CMSIncrBy(ctx, "bikes:profit",
    49  		"Rocky Mountain Racer", 200,
    50  		"Cloudy City Cruiser", 150,
    51  	).Result()
    52  
    53  	if err != nil {
    54  		panic(err)
    55  	}
    56  
    57  	fmt.Println(res3) // >>> [200 150]
    58  
    59  	res4, err := rdb.CMSQuery(ctx, "bikes:profit",
    60  		"Smoky Mountain Striker",
    61  	).Result()
    62  
    63  	if err != nil {
    64  		panic(err)
    65  	}
    66  
    67  	fmt.Println(res4) // >>> [100]
    68  
    69  	res5, err := rdb.CMSInfo(ctx, "bikes:profit").Result()
    70  
    71  	if err != nil {
    72  		panic(err)
    73  	}
    74  
    75  	fmt.Printf("Width: %v, Depth: %v, Count: %v",
    76  		res5.Width, res5.Depth, res5.Count)
    77  	// >>> Width: 2000, Depth: 9, Count: 450
    78  	// STEP_END
    79  
    80  	// Output:
    81  	// OK
    82  	// [100]
    83  	// [200 150]
    84  	// [100]
    85  	// Width: 2000, Depth: 9, Count: 450
    86  }
    87  

View as plain text