-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_store.go
72 lines (60 loc) · 1.6 KB
/
redis_store.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
package goutube
import (
"github.com/Brijeshlakkad/goutube/log"
"github.com/go-redis/redis"
"github.com/pelletier/go-toml"
)
// RedisPointStore manages points and its last offset
type RedisPointStore struct {
redis *redis.Client
log *log.Log // Backup to warm up the cache
}
type redisLogPoint struct {
Point string
Offset uint64
}
type redisPointValue struct {
PointOffset uint64
LogOffset uint64
}
func (rps *RedisPointStore) AddPointEvent(pointId string, offset uint64) error {
logOffset, err := rps.log.Append(&log.Record{Value: &redisLogPoint{Point: pointId, Offset: offset}})
// Store both log and point offsets.
pointValue := &redisPointValue{PointOffset: offset, LogOffset: logOffset}
t, err := toml.Marshal(pointValue)
if err != nil {
return err
}
return rps.redis.Set(pointId, t, 0).Err()
}
func (rps *RedisPointStore) GetPointEvent(pointId string) (uint64, error) {
t, err := rps.redis.Get(pointId).Result()
if err != nil {
return 0, err
}
var pointValue redisPointValue
err = toml.Unmarshal([]byte(t), &pointValue)
if err != nil {
return 0, err
}
return pointValue.PointOffset, err
}
func NewRedisPointStore(address string, dir string) (*RedisPointStore, error) {
client := redis.NewClient(&redis.Options{
Addr: address,
Password: "", // use empty password for simplicity. should come from a secret in production
DB: 0, // use default DB
})
_, err := client.Ping().Result()
if err != nil {
return nil, err
}
log, err := log.NewLog(dir, log.Config{})
if err != nil {
return nil, err
}
return &RedisPointStore{
redis: client,
log: log,
}, nil
}