/
redis.go
193 lines (142 loc) · 4.11 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
Copyright © 2023 Patrick Hermann patrick.hermann@sva.de
*/
package cli
import (
"context"
"fmt"
"log"
redigo "github.com/gomodule/redigo/redis"
"github.com/nitishm/go-rejson/v4"
"github.com/stuttgart-things/redisqueue"
"github.com/redis/go-redis/v9"
)
var redisClient *redis.Client
var ctx = context.Background()
func GetRedisJSON(redisJSONHandler *rejson.Handler, jsonKey string) (jsonObject []byte, jsonExists bool) {
jsonObject, err := redigo.Bytes(redisJSONHandler.JSONGet(jsonKey, "."))
if err != nil {
fmt.Println(err)
fmt.Println("Failed to JSONGet")
return
} else {
jsonExists = true
}
return
}
func SetRedisJSON(redisJSONHandler *rejson.Handler, jsonObject interface{}, jsonKey string) {
res, err := redisJSONHandler.JSONSet(jsonKey, ".", jsonObject)
if err != nil {
log.Fatalf("FAILED TO JSONSET")
return
}
if res.(string) == "OK" {
fmt.Printf("SUCCESS: %s\n", res)
} else {
fmt.Println("FAILED TO SET: ")
}
}
func DeleteRedisSet(redisClient *redis.Client, setKey string) (isSetDeleted bool) {
err := redisClient.Del(context.TODO(), setKey).Err()
if err != nil {
panic(err)
} else {
isSetDeleted = true
}
return
}
func AddValueToRedisSet(redisClient *redis.Client, setKey, value string) (isSetValueunique bool) {
if redisClient.SAdd(context.TODO(), setKey, value).Val() == 1 {
isSetValueunique = true
}
return
}
func GetValuesFromRedisSet(redisClient *redis.Client, setKey string) (values []string) {
values = redisClient.SMembers(context.TODO(), setKey).Val()
return
}
func GetValueFromRedisByKey(redisClient *redis.Client, key string) (value string) {
value, err := redisClient.Get(ctx, key).Result()
if err != nil {
panic(err)
}
return
}
func GetRandomValueFromRedis(redisClient *redis.Client, ctx context.Context, key string) string {
ranVal := redisClient.SRandMember(context.TODO(), key).Val()
return ranVal
}
func GetAllValuesFromRedis(redisClient *redis.Client, ctx context.Context, key string) []string {
rvalues, err := redisClient.SMembers(context.TODO(), key).Result()
if err != nil {
fmt.Println(err)
}
return rvalues
}
func RemoveValueFromRedis(redisClient *redis.Client, ctx context.Context, key string, member string) bool {
remVal, err := redisClient.SRem(context.TODO(), key, member).Result()
if err != nil {
fmt.Println(err)
}
if remVal == 1 {
return true
}
return false
}
func CreateRedisClient(connectionString, redisPassword string) (client *redis.Client) {
client = redis.NewClient(&redis.Options{
Addr: connectionString,
Password: redisPassword,
DB: 0,
})
return
}
func CheckRedisKV(connectionString, redisPassword, key, expectedValue string) (keyValueExists bool) {
rdb := CreateRedisClient(connectionString, redisPassword)
// CHECK IF KEY EXISTS IN REDIS
fmt.Println("CHECKING IF KEY " + key + " EXISTS..")
keyExists, err := rdb.Exists(context.TODO(), key).Result()
if err != nil {
panic(err)
}
// CHECK FOR VALUE/STATUS IN REDIS
if keyExists == 1 {
fmt.Println("KEY " + key + " EXISTS..CHECKING FOR IT'S VALUE")
value, err := rdb.Get(context.TODO(), key).Result()
if err != nil {
panic(err)
}
if value == expectedValue {
fmt.Println("STATUS", value)
keyValueExists = true
}
fmt.Println("STATUS", value)
} else {
fmt.Println("KEY " + key + " DOES NOT EXIST)")
}
return
}
func EnqueueDataInRedisStreams(connectionString, redisPassword, stream string, values map[string]interface{}) (enqueue bool) {
producer, err := redisqueue.NewProducerWithOptions(&redisqueue.ProducerOptions{
MaxLen: 10000,
ApproximateMaxLength: true,
RedisClient: redis.NewClient(&redis.Options{
Addr: connectionString,
Password: redisPassword,
DB: 0,
}),
})
if err != nil {
panic(err)
}
redisStreamErr := producer.Enqueue(&redisqueue.Message{
Stream: stream,
Values: values,
})
if redisStreamErr != nil {
panic(redisStreamErr)
} else {
enqueue = true
}
return
}