This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
/
redisstore.go
148 lines (125 loc) · 3.24 KB
/
redisstore.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
package store
import (
"context"
"errors"
"log"
"fmt"
"os"
"strings"
"time"
"github.com/go-redis/redis/v8"
"github.com/tidwall/gjson"
"github.com/miniscruff/dashy/configs"
)
func valueKey(name, value string) string {
return fmt.Sprintf("value:%v:%v", name, value)
}
func timeKey(name string) string {
return fmt.Sprintf("next-update:%v", name)
}
type RedisStore struct {
config *configs.Config
ctx context.Context
client *redis.Client
}
func NewRedisStore(config *configs.Config) (*RedisStore, error) {
var (
opts *redis.Options
err error
)
if config.Env.RedisUrl != "" {
opts, err = redis.ParseURL(config.Env.RedisUrl)
if err != nil {
return nil, err
}
} else {
if config.Env.RedisAddress == "" {
return nil, errors.New("missing REDIS_ADDRESS env var")
}
opts = &redis.Options{
Addr: config.Env.RedisAddress,
Username: config.Env.RedisUsername,
Password: config.Env.RedisPassword,
DB: config.Env.RedisDatabase,
}
}
client := redis.NewClient(opts)
return &RedisStore{
config: config,
client: client,
ctx: context.Background(),
}, nil
}
func (s *RedisStore) StringOrVar(value string) string {
// add redis:KEY as well for things like refresh tokens
if strings.HasPrefix(value, "env:") {
return os.Getenv(value[4:])
}
return value
}
func (s *RedisStore) GetNextRun(feed *configs.FeedConfig) (time.Time, error) {
timeStr, err := s.client.Get(s.ctx, timeKey(feed.Name)).Result()
if err == redis.Nil || err != nil {
return time.Time{}, nil
}
return time.Parse(timeFormat, timeStr)
}
func (s *RedisStore) SetNextRun(feed *configs.FeedConfig, nextRun time.Time) error {
timeFormatted := nextRun.Format(timeFormat)
_, err := s.client.Set(s.ctx, timeKey(feed.Name), timeFormatted, 0).Result()
return err
}
func (s *RedisStore) GetValues() (map[string]map[string]interface{}, error) {
pipe := s.client.Pipeline()
var keys []string
for _, feed := range s.config.Feeds {
for _, store := range feed.Store {
key := valueKey(feed.Name, store.Name)
if store.IsArray {
pipe.LRange(s.ctx, key, 0, -1)
} else {
pipe.Get(s.ctx, key)
}
keys = append(keys, feed.Name+"|"+store.Name)
}
}
cmds, err := pipe.Exec(s.ctx)
if err != nil {
log.Println(fmt.Errorf("unable to get data: %w\n", err))
return nil, err
}
data := make(map[string]map[string]interface{}, 0)
for i, cmd := range cmds {
split := strings.Split(keys[i], "|")
if _, exists := data[split[0]]; !exists {
data[split[0]] = make(map[string]interface{}, 0)
}
switch c := cmd.(type) {
case *redis.StringCmd:
data[split[0]][split[1]] = c.Val()
case *redis.StringSliceCmd:
data[split[0]][split[1]] = c.Val()
default:
fmt.Printf("unknown type: %v\n", c)
data[split[0]][split[1]] = c.String()
}
}
return data, nil
}
func (s *RedisStore) SetValues(feed *configs.FeedConfig, values map[string]gjson.Result) error {
pipe := s.client.Pipeline()
for k, result := range values {
key := valueKey(feed.Name, k)
if result.IsArray() {
for _, v := range result.Array() {
pipe.RPush(s.ctx, key, v.Value())
}
start := -int64(len(result.Array()))
pipe.LTrim(s.ctx, key, start, -1)
} else {
pipe.Set(s.ctx, key, result.Value(), 0)
}
}
_, err := pipe.Exec(s.ctx)
return err
}