-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddrm-redis.go
93 lines (69 loc) · 1.77 KB
/
ddrm-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
//go:build client
// +build client
package main
import (
"context"
"github.com/redis/go-redis/v9"
)
// Redis runtime context and database objects
var (
ctx = context.Background()
// We reinitialise this client if a startup config is present
rdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
)
func reinitRedis() (reinitialised bool) {
reinitialised = false
if ddrmAppConfig.RedisServer != rdb.Options().Addr {
rdb = redis.NewClient(&redis.Options{
Addr: ddrmAppConfig.RedisServer,
Password: ddrmAppConfig.RedisPassword,
DB: ddrmAppConfig.RedisDatabase,
})
reinitialised = true
}
return
}
func cacheKey(fqdn string, recordType DdrmRecordType) string {
return ddrmAppConfig.RedisKeyPrefix + ":" + fqdn + ":" + string(recordType)
}
// check Redis for current cached data
func getCachedValues(fqdn string, recordType DdrmRecordType) (answer []string) {
dbgf(ddrmDebugTryingCache, fqdn, string(recordType))
answer = []string{}
if stateUseRedis {
cachedKey := cacheKey(fqdn, recordType)
members, err := rdb.SMembers(ctx, cachedKey).Result()
if err != nil {
return answer
}
answer = members
return
}
dbgf(ddrmDebugNoCache, fqdn, string(recordType))
return
}
// save values in Redis as sets
func setCachedValues(fqdn string, recordType DdrmRecordType, answer []string) (success bool) {
success = false
if stateUseRedis {
cachedKey := cacheKey(fqdn, recordType)
// delete the key so we can reuse it
err := rdb.Del(ctx, cachedKey).Err()
if err != nil {
return
}
// add each answer one at a time because SAdd appears to be bugged
for _, a := range answer {
err = rdb.SAdd(ctx, cachedKey, a).Err()
if err != nil {
return
}
}
success = true
}
return
}