-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
267 lines (198 loc) · 5.74 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
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
263
264
265
266
267
/*
Copyright © 2023 Patrick Hermann [email protected]
*/
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) {
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
}
// 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
}