| 162 | } |
| 163 | |
| 164 | func ExampleClient_ttl_cmd() { |
| 165 | ctx := context.Background() |
| 166 | |
| 167 | rdb := redis.NewClient(&redis.Options{ |
| 168 | Addr: "localhost:6379", |
| 169 | Password: "", // no password docs |
| 170 | DB: 0, // use default DB |
| 171 | }) |
| 172 | |
| 173 | // REMOVE_START |
| 174 | // start with fresh database |
| 175 | rdb.FlushDB(ctx) |
| 176 | rdb.Del(ctx, "mykey") |
| 177 | // REMOVE_END |
| 178 | |
| 179 | // STEP_START ttl |
| 180 | ttlResult1, err := rdb.Set(ctx, "mykey", "Hello", 10*time.Second).Result() |
| 181 | |
| 182 | if err != nil { |
| 183 | panic(err) |
| 184 | } |
| 185 | |
| 186 | fmt.Println(ttlResult1) // >>> OK |
| 187 | |
| 188 | ttlResult2, err := rdb.TTL(ctx, "mykey").Result() |
| 189 | |
| 190 | if err != nil { |
| 191 | panic(err) |
| 192 | } |
| 193 | |
| 194 | fmt.Println(math.Round(ttlResult2.Seconds())) // >>> 10 |
| 195 | // STEP_END |
| 196 | |
| 197 | // Output: |
| 198 | // OK |
| 199 | // 10 |
| 200 | } |