/
redis.go
67 lines (58 loc) · 1.64 KB
/
redis.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
58
59
60
61
62
63
64
65
66
67
package redis
import (
"context"
"fmt"
"time"
"github.com/go-redis/redis/v8"
"go.k6.io/k6/js/modules"
)
func init() {
modules.Register("k6/x/redis", new(REDIS))
}
// REDIS is the k6 Redis extension.
type REDIS struct{}
// NewClient creates a new Redis client
func (*REDIS) NewClient(addr string, password string, bd int) *redis.Client {
if addr == "" {
addr = "localhost:6379"
}
return redis.NewClient(&redis.Options{
Addr: addr,
Password: password, // no password set
DB: bd, // use default DB
})
}
// Set adds a key/value
func (*REDIS) Set(client *redis.Client, key string, value interface{}, expiration time.Duration) {
// TODO: Make expiration configurable. Or document somewhere the unit.
err := client.Set(context.Background(), key, value, expiration*time.Second).Err()
if err != nil {
ReportError(err, "Failed to set the specified key/value pair")
}
}
// Get gets a key/value
func (*REDIS) Get(client *redis.Client, key string) string {
val, err := client.Get(context.Background(), key).Result()
if err != nil {
ReportError(err, "Failed to get the specified key")
}
return val
}
// Del removes a key/value
func (*REDIS) Del(client *redis.Client, key string) {
err := client.Del(context.Background(), key).Err()
if err != nil {
ReportError(err, "Failed to remove the specified key")
}
}
// Do runs arbitrary/custom commands
func (*REDIS) Do(client *redis.Client, args ...interface{}) (interface{}, error) {
val, err := client.Do(context.Background(), args...).Result()
if err != nil {
if err == redis.Nil {
return "", fmt.Errorf("key does not exist: %w", err)
}
return "", err
}
return val, nil
}