forked from romanyx/polluter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
40 lines (32 loc) · 752 Bytes
/
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
package polluter
import (
"encoding/json"
"github.com/go-redis/redis"
"github.com/pkg/errors"
"github.com/romanyx/jwalk"
)
type redisEngine struct {
cli *redis.Client
}
func (e redisEngine) exec(cmds []command) error {
for _, cmd := range cmds {
if err := e.cli.Set(cmd.q, cmd.args[0], 0).Err(); err != nil {
return errors.Wrap(err, "failed to set")
}
}
return nil
}
func (e redisEngine) build(obj jwalk.ObjectWalker) (commands, error) {
cmds := make(commands, 0)
if err := obj.Walk(func(key string, value interface{}) error {
data, err := json.Marshal(value)
if err != nil {
return err
}
cmds = append(cmds, command{key, []interface{}{data}})
return nil
}); err != nil {
return nil, err
}
return cmds, nil
}