-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis.go
More file actions
262 lines (194 loc) · 6.22 KB
/
Copy pathredis.go
File metadata and controls
262 lines (194 loc) · 6.22 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
Copyright © 2023 Patrick Hermann patrick.hermann@sva.de
*/
package cli
import (
"context"
"fmt"
"log"
"github.com/RediSearch/redisearch-go/redisearch"
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) error {
res, err := redisJSONHandler.JSONSet(jsonKey, ".", jsonObject)
if err != nil {
return fmt.Errorf("failed to JSONSet key %s: %w", jsonKey, err)
}
if res.(string) == "OK" {
fmt.Printf("SUCCESS: %s\n", res)
} else {
return fmt.Errorf("failed to set key %s: unexpected response %v", jsonKey, res)
}
return nil
}
func DeleteRedisSet(redisClient *redis.Client, setKey string) (isSetDeleted bool, err error) {
err = redisClient.Del(context.TODO(), setKey).Err()
if err != nil {
return false, fmt.Errorf("failed to delete redis set %s: %w", setKey, err)
}
return true, nil
}
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, err error) {
value, err = redisClient.Get(ctx, key).Result()
if err != nil {
return "", fmt.Errorf("failed to get redis key %s: %w", key, err)
}
return value, nil
}
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, err error) {
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 {
return false, fmt.Errorf("failed to check if key %s exists: %w", key, 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 {
return false, fmt.Errorf("failed to get value for key %s: %w", key, err)
}
if value == expectedValue {
fmt.Println("STATUS", value)
keyValueExists = true
}
fmt.Println("STATUS", value)
} else {
fmt.Println("KEY " + key + " DOES NOT EXIST)")
}
return keyValueExists, nil
}
func EnqueueDataInRedisStreams(connectionString, redisPassword, stream string, values map[string]interface{}) (enqueue bool, err error) {
producer, err := redisqueue.NewProducerWithOptions(&redisqueue.ProducerOptions{
MaxLen: 10000,
ApproximateMaxLength: true,
RedisClient: redis.NewClient(&redis.Options{
Addr: connectionString,
Password: redisPassword,
DB: 0,
}),
})
if err != nil {
return false, fmt.Errorf("failed to create redis producer: %w", err)
}
redisStreamErr := producer.Enqueue(&redisqueue.Message{
Stream: stream,
Values: values,
})
if redisStreamErr != nil {
return false, fmt.Errorf("failed to enqueue message to stream %s: %w", stream, redisStreamErr)
}
return true, nil
}
// CHECK IF REDISEARCH-INDEX EXISTS
func CheckIfRedisSearchIndexExists(client *redisearch.Client) (bool, error) {
_, err := client.Info()
if err != nil {
if err.Error() == "Unknown Index name" {
return false, nil
}
return false, err
}
return true, nil
}
// CREATE A REDIS CONNECTION POOL
func CreateRedisConnectionPool(redisHost, redisPassword string) (pool *redigo.Pool) {
pool = &redigo.Pool{
MaxIdle: 10,
MaxActive: 10,
Dial: func() (redigo.Conn, error) {
c, err := redigo.Dial("tcp", redisHost)
if err != nil {
return nil, err
}
if _, err := c.Do("AUTH", redisPassword); err != nil {
c.Close()
return nil, err
}
return c, nil
},
}
return
}
// CREATE REDISEARCH INDEX
func CreateRedisSearchIndex(client *redisearch.Client, schema *redisearch.Schema) {
if err := client.CreateIndex(schema); err != nil {
log.Fatalf("Could not create index: %v", err)
}
}
// INDEX DOCUMENT
func IndexDocument(client *redisearch.Client, document redisearch.Document) {
if err := client.Index(document); err != nil {
log.Fatalf("Could not index document: %v", err)
}
}
// DROP REDISEARCH INDEX
func DropRedisSearchIndex(client *redisearch.Client) {
client.Drop()
}
// EXECUTE REDISEARCH QUERY
func ExecuteRediSearchQuery(client *redisearch.Client, query *redisearch.Query) (docs []redisearch.Document, total int, err error) {
// SEARCH FOR DOCUMENTS
docs, total, err = client.Search(query)
if err != nil {
log.Fatalf("Could not search for documents: %v", err)
}
return
}