| 
 | 1 | +// EXAMPLE: cmds_generic  | 
 | 2 | +// HIDE_START  | 
 | 3 | +package example_commands_test  | 
 | 4 | + | 
 | 5 | +import (  | 
 | 6 | +	"context"  | 
 | 7 | +	"fmt"  | 
 | 8 | +	"math"  | 
 | 9 | +	"time"  | 
 | 10 | + | 
 | 11 | +	"github.com/redis/go-redis/v9"  | 
 | 12 | +)  | 
 | 13 | + | 
 | 14 | +// HIDE_END  | 
 | 15 | + | 
 | 16 | +func ExampleClient_del_cmd() {  | 
 | 17 | +	ctx := context.Background()  | 
 | 18 | + | 
 | 19 | +	rdb := redis.NewClient(&redis.Options{  | 
 | 20 | +		Addr:     "localhost:6379",  | 
 | 21 | +		Password: "", // no password docs  | 
 | 22 | +		DB:       0,  // use default DB  | 
 | 23 | +	})  | 
 | 24 | + | 
 | 25 | +	// REMOVE_START  | 
 | 26 | +	rdb.Del(ctx, "key1", "key2", "key3")  | 
 | 27 | +	// REMOVE_END  | 
 | 28 | + | 
 | 29 | +	// STEP_START del  | 
 | 30 | +	delResult1, err := rdb.Set(ctx, "key1", "Hello", 0).Result()  | 
 | 31 | + | 
 | 32 | +	if err != nil {  | 
 | 33 | +		panic(err)  | 
 | 34 | +	}  | 
 | 35 | + | 
 | 36 | +	fmt.Println(delResult1) // >>> OK  | 
 | 37 | + | 
 | 38 | +	delResult2, err := rdb.Set(ctx, "key2", "World", 0).Result()  | 
 | 39 | + | 
 | 40 | +	if err != nil {  | 
 | 41 | +		panic(err)  | 
 | 42 | +	}  | 
 | 43 | + | 
 | 44 | +	fmt.Println(delResult2) // >>> OK  | 
 | 45 | + | 
 | 46 | +	delResult3, err := rdb.Del(ctx, "key1", "key2", "key3").Result()  | 
 | 47 | + | 
 | 48 | +	if err != nil {  | 
 | 49 | +		panic(err)  | 
 | 50 | +	}  | 
 | 51 | + | 
 | 52 | +	fmt.Println(delResult3) // >>> 2  | 
 | 53 | +	// STEP_END  | 
 | 54 | + | 
 | 55 | +	// Output:  | 
 | 56 | +	// OK  | 
 | 57 | +	// OK  | 
 | 58 | +	// 2  | 
 | 59 | +}  | 
 | 60 | + | 
 | 61 | +func ExampleClient_expire_cmd() {  | 
 | 62 | +	ctx := context.Background()  | 
 | 63 | + | 
 | 64 | +	rdb := redis.NewClient(&redis.Options{  | 
 | 65 | +		Addr:     "localhost:6379",  | 
 | 66 | +		Password: "", // no password docs  | 
 | 67 | +		DB:       0,  // use default DB  | 
 | 68 | +	})  | 
 | 69 | + | 
 | 70 | +	// REMOVE_START  | 
 | 71 | +	rdb.Del(ctx, "mykey")  | 
 | 72 | +	// REMOVE_END  | 
 | 73 | + | 
 | 74 | +	// STEP_START expire  | 
 | 75 | +	expireResult1, err := rdb.Set(ctx, "mykey", "Hello", 0).Result()  | 
 | 76 | + | 
 | 77 | +	if err != nil {  | 
 | 78 | +		panic(err)  | 
 | 79 | +	}  | 
 | 80 | + | 
 | 81 | +	fmt.Println(expireResult1) // >>> OK  | 
 | 82 | + | 
 | 83 | +	expireResult2, err := rdb.Expire(ctx, "mykey", 10*time.Second).Result()  | 
 | 84 | + | 
 | 85 | +	if err != nil {  | 
 | 86 | +		panic(err)  | 
 | 87 | +	}  | 
 | 88 | + | 
 | 89 | +	fmt.Println(expireResult2) // >>> true  | 
 | 90 | + | 
 | 91 | +	expireResult3, err := rdb.TTL(ctx, "mykey").Result()  | 
 | 92 | + | 
 | 93 | +	if err != nil {  | 
 | 94 | +		panic(err)  | 
 | 95 | +	}  | 
 | 96 | + | 
 | 97 | +	fmt.Println(math.Round(expireResult3.Seconds())) // >>> 10  | 
 | 98 | + | 
 | 99 | +	expireResult4, err := rdb.Set(ctx, "mykey", "Hello World", 0).Result()  | 
 | 100 | + | 
 | 101 | +	if err != nil {  | 
 | 102 | +		panic(err)  | 
 | 103 | +	}  | 
 | 104 | + | 
 | 105 | +	fmt.Println(expireResult4) // >>> OK  | 
 | 106 | + | 
 | 107 | +	expireResult5, err := rdb.TTL(ctx, "mykey").Result()  | 
 | 108 | + | 
 | 109 | +	if err != nil {  | 
 | 110 | +		panic(err)  | 
 | 111 | +	}  | 
 | 112 | + | 
 | 113 | +	fmt.Println(expireResult5) // >>> -1ns  | 
 | 114 | + | 
 | 115 | +	expireResult6, err := rdb.ExpireXX(ctx, "mykey", 10*time.Second).Result()  | 
 | 116 | + | 
 | 117 | +	if err != nil {  | 
 | 118 | +		panic(err)  | 
 | 119 | +	}  | 
 | 120 | + | 
 | 121 | +	fmt.Println(expireResult6) // >>> false  | 
 | 122 | + | 
 | 123 | +	expireResult7, err := rdb.TTL(ctx, "mykey").Result()  | 
 | 124 | + | 
 | 125 | +	if err != nil {  | 
 | 126 | +		panic(err)  | 
 | 127 | +	}  | 
 | 128 | + | 
 | 129 | +	fmt.Println(expireResult7) // >>> -1ns  | 
 | 130 | + | 
 | 131 | +	expireResult8, err := rdb.ExpireNX(ctx, "mykey", 10*time.Second).Result()  | 
 | 132 | + | 
 | 133 | +	if err != nil {  | 
 | 134 | +		panic(err)  | 
 | 135 | +	}  | 
 | 136 | + | 
 | 137 | +	fmt.Println(expireResult8) // >>> true  | 
 | 138 | + | 
 | 139 | +	expireResult9, err := rdb.TTL(ctx, "mykey").Result()  | 
 | 140 | + | 
 | 141 | +	if err != nil {  | 
 | 142 | +		panic(err)  | 
 | 143 | +	}  | 
 | 144 | + | 
 | 145 | +	fmt.Println(math.Round(expireResult9.Seconds())) // >>> 10  | 
 | 146 | +	// STEP_END  | 
 | 147 | + | 
 | 148 | +	// Output:  | 
 | 149 | +	// OK  | 
 | 150 | +	// true  | 
 | 151 | +	// 10  | 
 | 152 | +	// OK  | 
 | 153 | +	// -1ns  | 
 | 154 | +	// false  | 
 | 155 | +	// -1ns  | 
 | 156 | +	// true  | 
 | 157 | +	// 10  | 
 | 158 | +}  | 
 | 159 | + | 
 | 160 | +func ExampleClient_ttl_cmd() {  | 
 | 161 | +	ctx := context.Background()  | 
 | 162 | + | 
 | 163 | +	rdb := redis.NewClient(&redis.Options{  | 
 | 164 | +		Addr:     "localhost:6379",  | 
 | 165 | +		Password: "", // no password docs  | 
 | 166 | +		DB:       0,  // use default DB  | 
 | 167 | +	})  | 
 | 168 | + | 
 | 169 | +	// REMOVE_START  | 
 | 170 | +	rdb.Del(ctx, "mykey")  | 
 | 171 | +	// REMOVE_END  | 
 | 172 | + | 
 | 173 | +	// STEP_START ttl  | 
 | 174 | +	ttlResult1, err := rdb.Set(ctx, "mykey", "Hello", 10*time.Second).Result()  | 
 | 175 | + | 
 | 176 | +	if err != nil {  | 
 | 177 | +		panic(err)  | 
 | 178 | +	}  | 
 | 179 | + | 
 | 180 | +	fmt.Println(ttlResult1) // >>> OK  | 
 | 181 | + | 
 | 182 | +	ttlResult2, err := rdb.TTL(ctx, "mykey").Result()  | 
 | 183 | + | 
 | 184 | +	if err != nil {  | 
 | 185 | +		panic(err)  | 
 | 186 | +	}  | 
 | 187 | + | 
 | 188 | +	fmt.Println(math.Round(ttlResult2.Seconds())) // >>> 10  | 
 | 189 | +	// STEP_END  | 
 | 190 | + | 
 | 191 | +	// Output:  | 
 | 192 | +	// OK  | 
 | 193 | +	// 10  | 
 | 194 | +}  | 
0 commit comments