-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathredis.go
84 lines (71 loc) · 2.43 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package utils
import (
"context"
"encoding/json"
"time"
"github.com/redis/go-redis/v9"
)
// Redis utility instance
var Redis RedisUtil
// RedisUtil is a utility struct for working with Redis
type RedisUtil struct{}
// redisPingTTL is the timeout duration for a Ping request to Redis
var redisPingTTL = 5 * time.Second
// RedisConfig specifies configuration options for connecting to a Redis server
type RedisConfig struct {
URL string
}
// RedisClient is a wrapper around go-redis Client type
type RedisClient struct {
*redis.Client
}
// New creates a new Redis client based on the provided RedisConfig
func (RedisUtil) New(config RedisConfig) (*RedisClient, error) {
opt, err := redis.ParseURL(config.URL)
if err != nil {
return nil, err
}
client := redis.NewClient(opt)
ctx, cancel := context.WithTimeout(context.Background(), redisPingTTL)
defer cancel()
if err := client.Ping(ctx).Err(); err != nil {
return nil, err
}
return &RedisClient{client}, err
}
// SetStruct sets the value for a key in Redis
// The value is marshalled to JSON, and an optional expiration time can be set
func (r *RedisClient) SetStruct(key string, value any, expiration time.Duration) error {
b, err := json.Marshal(value)
if err != nil {
return err
}
return r.Client.Set(context.TODO(), key, string(b), expiration).Err()
}
// SetNXStruct sets the value for a key in Redis if the key does not already exist
// The value is marshalled to JSON, and an optional expiration time can be set
// Returns a boolean indicating whether the key was set, and an error (if any)
func (r *RedisClient) SetNXStruct(key string, value any, expiration time.Duration) (bool, error) {
b, err := json.Marshal(value)
if err != nil {
return false, err
}
return r.Client.SetNX(context.TODO(), key, string(b), expiration).Result()
}
// GetStruct retrieves the value of a key as a JSON-encoded struct
// and unmarshal it into a provided result variable
func (r *RedisClient) GetStruct(key string, result any) error {
val, err := r.Client.Get(context.TODO(), key).Result()
if err != nil {
return err
}
return json.Unmarshal([]byte(val), result)
}
// JSONSet is a convenience wrapper around SetStruct
func (r *RedisClient) JSONSet(key string, value any, expiration time.Duration) error {
return r.SetStruct(key, value, expiration)
}
// JSONGet is a convenience wrapper around GetStruct
func (r *RedisClient) JSONGet(key string, result any) error {
return r.GetStruct(key, result)
}