-
Notifications
You must be signed in to change notification settings - Fork 9
/
example_test.go
57 lines (50 loc) · 1.27 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package redis_test
import (
"io"
"log"
"time"
"github.com/pascaldekloe/redis/v2"
)
func ExampleClient_SETWithOptions() {
// connection setup
var Redis = redis.NewDefaultClient[string, string]("rds1.example.com")
defer Redis.Close()
// execute command
ok, err := Redis.SETWithOptions("k", "v", redis.SETOptions{
Flags: redis.NX | redis.EX,
Expire: time.Minute,
})
if err != nil {
log.Print("command error: ", err)
return
}
// evaluate NX condition
if ok {
log.Print("new string expires in a minute")
} else {
log.Print("left existing string as is")
}
}
func ExampleListener() {
// connection setup
var RedisListener = redis.NewListener(redis.ListenerConfig{
Func: func(channel string, message []byte, err error) {
switch err {
case nil:
log.Printf("received %q on %q", message, channel)
case redis.ErrClosed:
log.Print("subscription establishment terminated")
case io.ErrShortBuffer: // see ListenerConfig BufferSize
log.Printf("message on %q skipped due size", channel)
default:
log.Print("subscription error: ", err)
// recovery attempts follow automatically
}
},
Addr: "rds1.example.com:6379",
})
defer RedisListener.Close()
// listen quickly
RedisListener.SUBSCRIBE("demo_channel")
time.Sleep(time.Millisecond)
}