forked from lovoo/goka
/
redis.go
181 lines (152 loc) · 3.52 KB
/
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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package redis
import (
"errors"
"fmt"
"strconv"
"github.com/lovoo/goka/storage"
redis "gopkg.in/redis.v5"
)
const (
offsetKey = "__offset"
)
type redisStorage struct {
client *redis.Client
hash string
}
// New creates a new Storage backed by Redis.
func New(client *redis.Client, hash string) (storage.Storage, error) {
if client == nil {
return nil, errors.New("invalid redis client")
}
if err := client.Ping().Err(); err != nil {
return nil, err
}
return &redisStorage{
client: client,
hash: hash,
}, nil
}
func (s *redisStorage) Has(key string) (bool, error) {
return s.client.HExists(s.hash, key).Result()
}
func (s *redisStorage) Get(key string) ([]byte, error) {
has, err := s.client.HExists(s.hash, key).Result()
if err != nil {
return nil, fmt.Errorf("error checking for existence in redis (key %s): %v", key, err)
} else if !has {
return nil, nil
}
value, err := s.client.HGet(s.hash, key).Bytes()
if err != nil {
return nil, fmt.Errorf("error getting from redis (key %s): %v", key, err)
}
return value, nil
}
func (s *redisStorage) GetOffset(defValue int64) (int64, error) {
data, err := s.Get(offsetKey)
if err != nil {
return 0, err
}
if data == nil {
return defValue, nil
}
value, err := strconv.ParseInt(string(data), 10, 64)
if err != nil {
return 0, fmt.Errorf("error decoding redis offset (%s): %v", string(data), err)
}
return value, nil
}
func (s *redisStorage) Set(key string, value []byte) error {
err := s.client.HSet(s.hash, key, value).Err()
if err != nil {
return fmt.Errorf("error setting to redis (key %s): %v", key, err)
}
return nil
}
func (s *redisStorage) SetOffset(offset int64) error {
return s.Set(offsetKey, []byte(strconv.FormatInt(offset, 10)))
}
func (s *redisStorage) Delete(key string) error {
return s.client.HDel(s.hash, key).Err()
}
func (s *redisStorage) Iterator() (storage.Iterator, error) {
var current uint64
var keys []string
var err error
keys, current, err = s.client.HScan(s.hash, current, "", 0).Result()
if err != nil {
return nil, err
}
return &redisIterator{
current: current,
keys: keys,
client: s.client,
hash: s.hash,
}, nil
}
func (s *redisStorage) IteratorWithRange(start, limit []byte) (storage.Iterator, error) {
var current uint64
var keys []string
var err error
keys, current, err = s.client.HScan(s.hash, current, string(start), 0).Result()
if err != nil {
return nil, err
}
return &redisIterator{
current: current,
keys: keys,
client: s.client,
hash: s.hash,
}, nil
}
func (s *redisStorage) Recovered() bool {
return false
}
func (s *redisStorage) MarkRecovered() error {
return nil
}
func (s *redisStorage) Open() error {
return nil
}
func (s *redisStorage) Close() error {
return nil
}
type redisIterator struct {
current uint64
keys []string
client *redis.Client
hash string
}
func (i *redisIterator) exhausted() bool {
return uint64(len(i.keys)) <= i.current
}
func (i *redisIterator) Next() bool {
i.current++
if string(i.Key()) == offsetKey {
i.current++
}
return !i.exhausted()
}
func (i *redisIterator) Key() []byte {
if i.exhausted() {
return nil
}
key := i.keys[i.current]
return []byte(key)
}
func (i *redisIterator) Err() error {
return nil
}
func (i *redisIterator) Value() ([]byte, error) {
if i.exhausted() {
return nil, nil
}
key := i.keys[i.current]
return i.client.HGet(i.hash, key).Bytes()
}
func (i *redisIterator) Release() {
i.current = uint64(len(i.keys))
}
func (i *redisIterator) Seek(key []byte) bool {
return !i.exhausted()
}