...

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

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

     1  // EXAMPLE: topk_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_topk() {
    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  	// start with fresh database
    25  	rdb.FlushDB(ctx)
    26  	rdb.Del(ctx, "bikes:keywords")
    27  	// REMOVE_END
    28  
    29  	// STEP_START topk
    30  	res1, err := rdb.TopKReserve(ctx, "bikes:keywords", 5).Result()
    31  
    32  	if err != nil {
    33  		panic(err)
    34  	}
    35  
    36  	fmt.Println(res1) // >>> OK
    37  
    38  	res2, err := rdb.TopKAdd(ctx, "bikes:keywords",
    39  		"store",
    40  		"seat",
    41  		"handlebars",
    42  		"handles",
    43  		"pedals",
    44  		"tires",
    45  		"store",
    46  		"seat",
    47  	).Result()
    48  
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  
    53  	fmt.Println(res2) // >>> [     handlebars  ]
    54  
    55  	res3, err := rdb.TopKList(ctx, "bikes:keywords").Result()
    56  
    57  	if err != nil {
    58  		panic(err)
    59  	}
    60  
    61  	fmt.Println(res3) // [store seat pedals tires handles]
    62  
    63  	res4, err := rdb.TopKQuery(ctx, "bikes:keywords", "store", "handlebars").Result()
    64  
    65  	if err != nil {
    66  		panic(err)
    67  	}
    68  
    69  	fmt.Println(res4) // [true false]
    70  	// STEP_END
    71  
    72  	// Output:
    73  	// OK
    74  	// [     handlebars  ]
    75  	// [store seat pedals tires handles]
    76  	// [true false]
    77  }
    78  

View as plain text