...

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

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

     1  // EXAMPLE: bf_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_bloom() {
    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:models")
    27  	// REMOVE_END
    28  
    29  	// STEP_START bloom
    30  	res1, err := rdb.BFReserve(ctx, "bikes:models", 0.01, 1000).Result()
    31  
    32  	if err != nil {
    33  		panic(err)
    34  	}
    35  
    36  	fmt.Println(res1) // >>> OK
    37  
    38  	res2, err := rdb.BFAdd(ctx, "bikes:models", "Smoky Mountain Striker").Result()
    39  
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  
    44  	fmt.Println(res2) // >>> true
    45  
    46  	res3, err := rdb.BFExists(ctx, "bikes:models", "Smoky Mountain Striker").Result()
    47  
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  
    52  	fmt.Println(res3) // >>> true
    53  
    54  	res4, err := rdb.BFMAdd(ctx, "bikes:models",
    55  		"Rocky Mountain Racer",
    56  		"Cloudy City Cruiser",
    57  		"Windy City Wippet",
    58  	).Result()
    59  
    60  	if err != nil {
    61  		panic(err)
    62  	}
    63  
    64  	fmt.Println(res4) // >>> [true true true]
    65  
    66  	res5, err := rdb.BFMExists(ctx, "bikes:models",
    67  		"Rocky Mountain Racer",
    68  		"Cloudy City Cruiser",
    69  		"Windy City Wippet",
    70  	).Result()
    71  
    72  	if err != nil {
    73  		panic(err)
    74  	}
    75  
    76  	fmt.Println(res5) // >>> [true true true]
    77  	// STEP_END
    78  
    79  	// Output:
    80  	// OK
    81  	// true
    82  	// true
    83  	// [true true true]
    84  	// [true true true]
    85  }
    86  

View as plain text