1package cursor
2
3import (
4 "context"
5 "fmt"
6 "strconv"
7
8 "tangled.sh/tangled.sh/core/appview/cache"
9)
10
11const (
12 cursorKey = "cursor:%s"
13)
14
15type RedisStore struct {
16 rdb *cache.Cache
17}
18
19func NewRedisCursorStore(cache *cache.Cache) RedisStore {
20 return RedisStore{
21 rdb: cache,
22 }
23}
24
25func (r *RedisStore) Set(knot string, cursor int64) {
26 key := fmt.Sprintf(cursorKey, knot)
27 r.rdb.Set(context.Background(), key, cursor, 0)
28}
29
30func (r *RedisStore) Get(knot string) (cursor int64) {
31 key := fmt.Sprintf(cursorKey, knot)
32 val, err := r.rdb.Get(context.Background(), key).Result()
33 if err != nil {
34 return 0
35 }
36 cursor, err = strconv.ParseInt(val, 10, 64)
37 if err != nil {
38 // TODO: log here
39 return 0
40 }
41
42 return cursor
43}