-
Notifications
You must be signed in to change notification settings - Fork 3
/
redis_persistence.go
70 lines (60 loc) · 1.63 KB
/
redis_persistence.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
package loggingdrain
import (
"context"
"encoding/json"
"time"
"github.com/redis/go-redis/v9"
)
type RedisPersistence struct {
addr string
password string
db int
serviceKey string
rdb RedisClient
}
// RedisClient for mock testing
type RedisClient interface {
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
Get(ctx context.Context, key string) *redis.StringCmd
Subscribe(ctx context.Context, channels ...string) *redis.PubSub
}
var _ RedisClient = &redis.Client{}
func NewRedisPersistence(addr, password string, db int, serviceKey string) *RedisPersistence {
rdb := redis.NewClient(&redis.Options{
Addr: addr,
Password: password,
DB: db,
})
return &RedisPersistence{
addr: addr,
password: password,
db: db,
serviceKey: serviceKey,
rdb: rdb,
}
}
var _ persistenceHandler = &RedisPersistence{}
func (p *RedisPersistence) Save(ctx context.Context, template *TemplateMiner) error {
b, err := json.Marshal(template)
if err != nil {
return errInternal(err)
}
if err := p.rdb.Set(ctx, p.serviceKey, string(b), 0).Err(); err != nil {
return errInternal(err)
}
return nil
}
func (p *RedisPersistence) Load(ctx context.Context) (*TemplateMiner, error) {
val, err := p.rdb.Get(ctx, p.serviceKey).Result()
if err != nil {
return nil, errInternal(err)
}
miner := TemplateMiner{}
if err := json.Unmarshal([]byte(val), &miner); err != nil {
return nil, errInternal(err)
}
return &miner, nil
}
func (p *RedisPersistence) Subscribe(ctx context.Context) *redis.PubSub {
return p.rdb.Subscribe(ctx, p.serviceKey)
}