Skip to content

Commit 71605f6

Browse files
DOC-4328 added DEL, EXPIRE, and TTL command examples (#3143)
Co-authored-by: ofekshenawa <[email protected]>
1 parent e69895b commit 71605f6

File tree

3 files changed

+285
-5
lines changed

3 files changed

+285
-5
lines changed

.github/workflows/build.yml

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ permissions:
1010
contents: read
1111

1212
jobs:
13-
build:
14-
name: build
13+
test-makefile:
14+
name: Build with Makefile
1515
runs-on: ubuntu-latest
16+
1617
strategy:
1718
fail-fast: false
1819
matrix:
@@ -35,11 +36,94 @@ jobs:
3536
- name: Checkout code
3637
uses: actions/checkout@v4
3738

38-
- name: Test
39+
- name: Run tests using Makefile
3940
run: make test
4041

4142
- name: Upload to Codecov
4243
uses: codecov/codecov-action@v4
4344
with:
4445
files: coverage.txt
45-
token: ${{ secrets.CODECOV_TOKEN }}
46+
token: ${{ secrets.CODECOV_TOKEN }}
47+
48+
test-redis-8:
49+
name: Build with Enhanced Tests
50+
runs-on: ubuntu-latest
51+
52+
strategy:
53+
fail-fast: false
54+
matrix:
55+
go-version: [1.19.x, 1.20.x, 1.21.x]
56+
57+
services:
58+
redis:
59+
image: redis:8.0-M01
60+
options: >-
61+
--health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5
62+
ports:
63+
- 6379:6379
64+
- 6380:6380
65+
- 6381:6381
66+
- 6390:6390
67+
- 6391:6391
68+
- 6392:6392
69+
- 9123:9123
70+
- 9124:9124
71+
- 9125:9125
72+
- 9126:9126
73+
- 9127:9127
74+
- 9128:9128
75+
- 8220:8220
76+
- 8221:8221
77+
- 8222:8222
78+
- 8223:8223
79+
- 8224:8224
80+
- 8225:8225
81+
82+
steps:
83+
- name: Set up ${{ matrix.go-version }}
84+
uses: actions/setup-go@v5
85+
with:
86+
go-version: ${{ matrix.go-version }}
87+
88+
- name: Checkout code
89+
uses: actions/checkout@v4
90+
91+
- name: Run enhanced tests
92+
run: |
93+
GO_MOD_DIRS=$(find . -type f -name 'go.mod' -exec dirname {} \; | sort)
94+
GO_VERSION=$(go version | cut -d " " -f 3 | cut -d. -f2)
95+
set -e
96+
for dir in $GO_MOD_DIRS; do
97+
if echo "$dir" | grep -q "./example" && [ "$GO_VERSION" = "19" ]; then
98+
echo "Skipping go test in $dir due to Go version 1.19 and dir contains ./example"
99+
continue
100+
fi
101+
echo "Running tests in $dir"
102+
cd "$dir"
103+
go mod tidy -compat=1.18
104+
go test --ginkgo.skip-file="gears_commands_test.go"
105+
go test ./... -short -race --ginkgo.skip-file="gears_commands_test.go"
106+
go test ./... -run=NONE -bench=. -benchmem --ginkgo.skip-file="gears_commands_test.go"
107+
env GOOS=linux GOARCH=386 go test --ginkgo.skip-file="gears_commands_test.go"
108+
go test -coverprofile=coverage.txt -covermode=atomic ./... --ginkgo.skip-file="gears_commands_test.go"
109+
go vet
110+
cd -
111+
done
112+
113+
- name: Build and run custom vet tool
114+
run: |
115+
cd internal/customvet
116+
go build .
117+
cd ../..
118+
go vet -vettool ./internal/customvet/customvet
119+
120+
- name: Check code formatting
121+
run: |
122+
gofumpt -w ./
123+
goimports -w -local github.com/redis/go-redis ./
124+
125+
- name: Upload to Codecov
126+
uses: codecov/codecov-action@v4
127+
with:
128+
files: coverage.txt
129+
token: ${{ secrets.CODECOV_TOKEN }}

commands_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,9 @@ var _ = Describe("Commands", func() {
679679
Expect(set.Val()).To(Equal("OK"))
680680

681681
migrate = client.Migrate(ctx, "localhost", redisSecondaryPort, "key", 0, 0)
682-
Expect(migrate.Err()).To(MatchError("IOERR error or timeout writing to target instance"))
682+
Expect(migrate.Err()).To(SatisfyAny(
683+
MatchError("IOERR error or timeout writing to target instance"),
684+
MatchError("IOERR error or timeout reading to target instance")))
683685
Expect(migrate.Val()).To(Equal(""))
684686
})
685687

doctests/cmds_generic_test.go

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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

Comments
 (0)