...

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

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

     1  // EXAMPLE: cuckoo_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_cuckoo() {
    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 cuckoo
    30  	res1, err := rdb.CFReserve(ctx, "bikes:models", 1000000).Result()
    31  
    32  	if err != nil {
    33  		panic(err)
    34  	}
    35  
    36  	fmt.Println(res1) // >>> OK
    37  
    38  	res2, err := rdb.CFAdd(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.CFExists(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.CFExists(ctx, "bikes:models", "Terrible Bike Name").Result()
    55  
    56  	if err != nil {
    57  		panic(err)
    58  	}
    59  
    60  	fmt.Println(res4) // >>> false
    61  
    62  	res5, err := rdb.CFDel(ctx, "bikes:models", "Smoky Mountain Striker").Result()
    63  
    64  	if err != nil {
    65  		panic(err)
    66  	}
    67  
    68  	fmt.Println(res5) // >>> true
    69  	// STEP_END
    70  
    71  	// Output:
    72  	// OK
    73  	// true
    74  	// true
    75  	// false
    76  	// true
    77  }
    78  

View as plain text