-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
47 lines (41 loc) · 977 Bytes
/
db.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
package main
import (
"log"
)
// save key value to Redis
func SaveToDB(key string, value []byte) error {
err := DB.Set(key, value, 0).Err()
if err != nil {
log.Println("Error occurred with save to Redis:", err)
return err
}
return nil
}
// load data from Redis by filtered key
func LoadFromDB(filter string) ([]string, error) {
keys, err := DB.Keys(filter).Result()
if err != nil {
log.Println("Error occurred with loading data from Redis:", err)
return nil, err
}
values := make([]string, len(keys))
for i, key := range keys {
value, err := DB.Get(key).Result()
if err != nil {
log.Printf(
"Error occurred with getting value by key %s: %s. Skip...", key, err)
continue
}
values[i] = value
}
return values, nil
}
// delete value by key from Redis
func DeleteFromDB(key string) error {
err := DB.Del(key).Err()
if err != nil {
log.Printf("Error occurred with deleting value by key %s: %s", key, err)
return err
}
return nil
}